I am trying to create a new file with the information that i will extract from my BD.
CONCATENATE cdf ndf
INTO cdf.
OPEN DATASET cdf for APPENDING IN LEGACY TEXT MODE WITH SMART LINEFEED.
IF sy-subrc = 0.
TRANSFER wa_es_tabt TO ndf.
CLOSE DATASET cdf.
ENDIF.
CDF = the path of my file with the name that the user will put
NDF = the name the user will put
wa_es_tabt = db information that I intend to save as txt.
but the sy-subrc is always 8, file not open... why?
thanks...
I did solve my problem this is the example of my program to create an save an txt file in ABAP.
FORM g_fich TABLES dt_es_tab STRUCTURE es_tabd
USING str TYPE string
str1 TYPE string.
CONCATENATE cdf ndf '.txt'
INTO str.
CONCATENATE 'Parceiro' 'Conta Contrato' 'Conttrato' 'Nome' 'Morada'
INTO str1
SEPARATED BY space.
OPEN DATASET str FOR APPENDING IN TEXT MODE ENCODING DEFAULT WITH SMART LINEFEED .
IF sy-subrc = 0.
LOOP AT dt_es_tab INTO wa_es_tabt.
IF cont = 0.
TRANSFER str1 TO str.
cont = 2.
ENDIF.
TRANSFER wa_es_tabt TO str.
ENDLOOP.
ENDIF.
CLOSE DATASET str.
ENDFORM. " G_FIC
CDF = the path of my file with the name that the user will put
NDF = the name the user will put
wa_es_tabt = data from BD that I intend to save as txt.
the problem with the old one was that i was intend to create a new file without an extension, like ".txt", and
I was trying to open an file whit just hes name.
thanks...
Well... That's not the point. You simple have to use the identical destination in both OPEN DATASET and TRANSFER statement.
eg:
OPEN DATASET abc FOR OUTPUT/APPENDING.
TRANSFER <whatever_you_want> TO abc.
In your first example you had the OPEN DATASET for cdf but the transfer to ndf which wasn't open for transfer at all.
The latter example only works because you use both times the destination variable str for both OPEN DATASET and TRANSFER.
Related
I have a whole bunch of text files that are exported from Photoshop that I need to import into an Excel document. I wrote a macro to get the job done and it seemed to work just fine for my test document but when I tried loading in some of the actual files produced by Photoshop Excel started putting all the data in a separate column except for the first line.
My code that reads the text file:
Open currentDocPath For Input As stream
Do Until EOF(stream)
Input #stream, currentLine
columnContents = Split(currentLine, vbTab)
For n = 0 To UBound(columnContents)
ActiveSheet.Cells(row, Chr(64 + colum + n)).Value = columnContents(n)
Next n
row = row + 1
Loop
Close stream
The text files I am reading look like this, only with much more data:
"Name" "Data" "Info" "blah"
"Name1" "Data1" "Info1" "blah1"
"Name2" "Data2" "Info2" "blah2"
The problem seemed pretty trivial, but when I load it into excel, instaed of looking like it does above it looks like this:
ÿþ"Name" "Data" "Info" "blah"
Name1
Data1
Info1
blah1
Name2
Data2
Info2
blah2
Now I am not sure why this is happening. It seems like the first two characters in the first row are there because those bytes declare the text encoding. Somehow those characters keep the first row formatted correctly while the remaining rows lose their quotation marks and all get moved to new lines.
Could someone who understands UCS-2 Little Endian text encoding explain how I can work around this? When I convert the files to ASCII it works fine.
Cheers!
edit: Okay so I understand now that the encoding is UTF-16 (I don't know a whole lot about character encoding). My main issue is that it's formatting strangely and I don't understand why or how to fix it. Thanks!
As I mentioned in my comment, it appears the file you're trying to import is encoded in UTF-16.
In this vbaexpress.com article, someone suggested that the following should work:
Dim GetOpenFile As String
Dim MyData As String
Dim r As Long
GetOpenFile = Application.GetOpenFilename
r = 1
Open GetOpenFile For Input As #1
Do While Not EOF(1)
Line Input #1, MyData
Cells(r, 1).Value = MyData
r = r + 1
Loop
Close #1
Obviously I can't test it myself, but maybe it'll help you.
Why not just tell excel to import the file. MS has probably put hundreds of thousands of person hours into that code. Record the importation to get easy code.
Remember Excel is a tool for non programmers to do programming things. Use it instead of trying to replace it.
These are the replacement file functions that you use for new code. Add a reference to Microsoft Scripting Runtime.
Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.
object.OpenTextFile(filename[, iomode[, create[, format]]])
Arguments
object
Required. Object is always the name of a FileSystemObject.
filename
Required. String expression that identifies the file to open.
iomode
Optional. Can be one of three constants: ForReading, ForWriting, or ForAppending.
create
Optional. Boolean value that indicates whether a new file can be created if the specified filename doesn't exist. The value is True if a new file is created, False if it isn't created. If omitted, a new file isn't created.
format
Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.
The format argument can have any of the following settings:
Constant Value Description
TristateUseDefault
-2
Opens the file using the system default.
TristateTrue
-1
Opens the file as Unicode.
TristateFalse
0
Opens the file as ASCII.
I would like to write some data to a csv file. I am using this code:
Dim Filename As String, line As String
Dim A As Integer
Filename = "D:" & "\testfile.csv"
Open Filename For Output As #1
For A = 1 To 100
Print #1, "test, test, test"
Next A
Close #1
but the problem is, that this code rewrite this cvs file from the beginning. but I would like to add data at the end of csv file ( For example if I run this code three times, I would like to have 300 lines in this csv file)
what should I do?
In which case you need Open Filename For Append As #1.
You might also find that Write #1, behaves better than Print #1, if you line contains quotation characters.
One last thing, don't hardcode the #1 as someone else may be using that handle. Instead, use
Dim n as Integer
n = Freefile 'Let VBA find a free file handle
'use #n rather than #1 from here.
Here is your Error:
Open Filename For Output As #1
which should be:
Open Filename For Append As #1
This will append your new text to the end of a stream.
I have this txt file with the following information:
National_Insurence_Number;Name;Surname;Hours_Worked;Price_Per_Hour so:
eg.: aa-12-34-56-a;Peter;Smith;36;12
This data has been inputed to the txt file through a VB form which works totally fine, the problem comes when, on another form. This is what I expect it to do:
The user will input into a text box the employees NI Number.
The program will then search through the file that NI Number and, if found;
It will fill in the appropriate text boxes with its data.
(Then the program calculates tax and national insurance which i got working fine)
So basically the problem comes telling the program to search that NI number and introduce each ";" delimited field into its corresponding text box.
Thanks for all.
You just need to parse the file like a csv, you can use Microsoft.VisualBasic.FileIO.TextFieldParser to do this or you can use CSVHelper - https://github.com/JoshClose/CsvHelper
I've used csv helper in the past and it works great, it allows you to create a class with the structure of the records in your data file then imports the data into a list of these for searching.
You can look here for more info on TextFieldParser if you want to go that way -
Parse Delimited CSV in .NET
Dim afile As FileIO.TextFieldParser = New FileIO.TextFieldParser(FileName)
Dim CurrentRecord As String() ' this array will hold each line of data
afile.TextFieldType = FileIO.FieldType.Delimited
afile.Delimiters = New String() {";"}
afile.HasFieldsEnclosedInQuotes = True
' parse the actual file
Do While Not afile.EndOfData
Try
CurrentRecord = afile.ReadFields
Catch ex As FileIO.MalformedLineException
Stop
End Try
Loop
I'd recommend using CsvHelper though, the documentation is pretty good and working with objects is much easier opposed to the raw string data.
Once you have found the record you can then manually set the text of each text box on your form or use a bindingsource.
What is the best was I can open a txt file in a SSIS and store the first line as a variable.
I want to put a list of accounts in the txt file and use them in a SQL task.
Refer to the link #Ozren gave you, to create a proper flat file connection e.g myfile and variable e.g.HeaderLine. Then create a script task, put HeaderLine var in read/write variables and code it with:
System.IO.StreamReader file =
new System.IO.StreamReader(Dts.Connections["myfile"].ConnectionString);
Dts.Variables["HeaderLine"].Value = file.ReadLine();
file.Close();
That's pretty much it, then you can put a standard DataFlow to read filedata from file to DB or resultset.
You'll have the first line in HeaderLine variable, which you can use anywhere you want in the SSIS package.
i want to create a notepad in this format
100001|10001|1001|91|9942321400|MR|Hari|Q|PUBLIC|249 MUNDON ROAD|MALDON|FL|44|TN|NO_PROVINCE|600004|IN|M|27304|9942321400|test#test.com|nothing|COMMENTS|1|Southeast
100001|10001|1001|91|9865015695|MR|Hari|Q|PUBLIC|249 MUNDON ROAD|MALDON|FL|44|TN|NO_PROVINCE|600004|IN|M|27304|9942321400|test#test.com|nothing|COMMENTS|1|Southeast
100001|10001|1001|91|9894825469|MR|Hari|Q|PUBLIC|249 MUNDON ROAD|MALDON|FL|44|TN|NO_PROVINCE|600004|IN|M|27304|9942321400|test#test.com|nothing|COMMENTS|1|Southeast
am using vb.net,in the above format except phone number remaining values are same.and it would be written in to the notepad with pipeline separator.
phone number values are recieved from dataset and store it in a variable . with in looping am use stream writer statement. but am not able to get the output. please help me to do this? am new to vb.net
Try something like that to write your file.
Import System.IO and add that routine. The Path should be the complete path including the file name and extension of the file you want to generate. If you change False to True it will append the text at the final of the file if you write it again, with false it overwrite it.
Using writer As StreamWriter = New StreamWriter(path, False, System.Text.Encoding.UTF8)
for each ...
writer.writeLine(var1+"|"+var2...)
Next
writer.Close()
End Using