I have a csv file, "List.csv", comprised of a single item per record. I would like a technique/script (preferably a VBScript) that will append a standard string to each record. The modified file will be saved to a single new file "Output.csv".
For example:
List.csv contains:
aaa
bbb
ccc
Standard string = XXXXX
Output.csv
XXXXXaaa
XXXXXbbb
XXXXXccc
Many thanks for help.
k
concatonate string to each record of a csv file
Moderators: JRL, Dorian (MJT support)
Not VBScript but should be easy to follow:
Code: Select all
//Path and file name for your input and output files
Let>file1=list.csv
Let>file2=Output.csv
//String to append
Let>PreString=XXXXX
//Read the input file and separate it by each line
ReadFile>%file1%,Data
Separate>Data,%CRLF%,Line
Let>kk=0
Repeat>kk
Add>kk,1
Let>value=Line_%kk%
//Write each appended line to a file
WriteLn>%file2%,wres,%PreString%%value%
message>Writing line %kk% of %Line_Count%
Until>kk,%Line_Count%
MDL>Complete, %kk% lines written.
Wonderful, wonderful, wonderful!!!
There is a small problem with the script though, as it writes a final extra "prestring" to the output file.
If I have a header row in the list how do I start the append in line 2?
Also why do I need to delete 2 lines to eliminate the macro complete message?
Thanks 10^6
k
There is a small problem with the script though, as it writes a final extra "prestring" to the output file.
If I have a header row in the list how do I start the append in line 2?
Also why do I need to delete 2 lines to eliminate the macro complete message?
Thanks 10^6
k
That means you have a blank line at the end of your csv file. There are several ways to handle this, it all depends on the result you want. You could live with the last line as it is being written now. You could remove the blank line from the file manually. You could subtract one from the "Line_count" variable so that the last blank line does not get processed. You could test each line to see if it contains data then only write those that do. You could test only the last line to see if it has data then either write it blank to the new file or not write it at all.There is a small problem with the script though, as it writes a final extra "prestring" to the output file.
Or what I would do is test "value" and if it's blank write a blank line.
...
Repeat>kk
Add>kk,1
Let>value=Line_%kk%
//Write each appended line to a file
If>%value%=
WriteLn>%file2%,wres,%value%
Else
WriteLn>%file2%,wres,%PreString%%value%
EndIf
message>Writing line %kk% of %Line_Count%
Until>kk,%Line_Count%
...
Write the first line of the input file to the output file before the Repeat>kk line in the script. Then change the start of the count from line one to line two by incrementing up the initial value of "kk" by one.If I have a header row in the list how do I start the append in line 2?
Put it all together....
Separate>Data,%CRLF%,Line
WriteLn>%file2%,wres,%Line_1%
Let>kk=1
Repeat>kk
...
Code: Select all
//Path and file name for your input and output files
Let>file1=list.csv
Let>file2=Output.csv
//String to append
Let>PreString=XXXXX
//Read the input file and separate it by each line
ReadFile>%file1%,Data
Separate>Data,%CRLF%,Line
WriteLn>%file2%,wres,%Line_1%
Let>kk=1
Repeat>kk
Add>kk,1
Let>value=Line_%kk%
//Write each appended line to a file
If>%value%=
WriteLn>%file2%,wres,%value%
Else
WriteLn>%file2%,wres,%PreString%%value%
EndIf
message>Writing line %kk% of %Line_Count%
Until>kk,%Line_Count%
MDL>Complete, %kk% lines written.