concatonate string to each record of a csv file

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

concatonate string to each record of a csv file

Post by kriemer » Fri Oct 30, 2009 7:51 pm

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

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Oct 30, 2009 9:12 pm

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.

kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

Post by kriemer » Fri Oct 30, 2009 9:37 pm

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

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Oct 30, 2009 10:03 pm

There is a small problem with the script though, as it writes a final extra "prestring" to the output file.
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.

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%
...
If I have a header row in the list how do I start the append in line 2?
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.
...
Separate>Data,%CRLF%,Line
WriteLn>%file2%,wres,%Line_1%

Let>kk=1
Repeat>kk
...
Put it all together.

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.

kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

Post by kriemer » Fri Oct 30, 2009 10:40 pm

So cool!

Thanks again.

k

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts