Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add data to line in CSV file

Status
Not open for further replies.

Leosy

Technical User
Apr 13, 2012
49
PL
Hello.

Coyld you help me.

I have some data in CSV

I need to open this CVS file via VBS script and add some data in the end of first line and save this file. For example

aaa;bbb;ccc
111;111;111
222;222;222

should be:

aaa;bbb;ccc;ddd;eee;fff
111;111;111
222;222;222

Best Regards
L
 
CSV files are just comma separated text. use the FileSystemObject like you would for any other text file.

Should we assume the [tt]aaa;bbb;ccc[/tt] is the data in one column as it is separated by semi-colons instead of commas? I assume adding [tt]ddd;eee;fff[/tt] conceptual for "How do I add data to a row in a CSV"? - or do you really want to add that to the end of [tt]aaa;bbb;ccc[/t]. Also, the data in a CSV column is typically encapsulated by quotes - I see none in this example.

-Geates



"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Yes it's semi-colons - logs (data) from servers. And we need to add just 3 new columns to the first (header) line. So in for example excel this file will be seen as 3 new columns but in csv it will be just

aaa;bbb;ccc;ddd;eee;fff
111;111;111
222;222;222
 
Will append ";ddd;eee;fff" to the first line of a file. That's all this script will do

Code:
set objFSO = CreateObject("Scripting.FileSystemObject")
set objInFile = objFSO.OpenTextFile("C:\myfile.log", 1, true, 0)

strContents = objInFile.ReadAll
objInFile.Close

arrRows = split(strContents, vbNewLine)
arrRows(0) = arrRows(0) & ";ddd;eee;fff"
strContents = join(arrRows, vbNewLine)

set objOutFile = objFSO.OpenTextFile("C:\myfile.log", 2, true, 0)
objOutFile.WriteLine strContents
objOutFile.close

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thank you very much ! it works !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top