Adding Headers to a csv file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
stylishjm
Posts: 10
Joined: 10 Feb 2012 03:34

Adding Headers to a csv file?

#1 Post by stylishjm » 14 Feb 2012 03:34

Following on from my earlier post regarding adding an extra column of information to a csv, I now need to get headers added to each column.
There are 40 columns, and have the header names in a .txt file, one per line.
Is there any easy way to add these in using a batch file?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Adding Headers to a csv file?

#2 Post by foxidrive » 14 Feb 2012 03:56

file.txt contains fields, one per line.

oldfile.csv contains the original data

newfile.csv is create with the header line and the data

Code: Select all

@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in ('type "file.txt"') do (
set "var=!var!,%%a"
)
set "var=%var:~1%"
echo %var%>"newfile.csv"
type "oldfile.csv">>"newfile.csv"
Last edited by foxidrive on 14 Feb 2012 04:02, edited 1 time in total.

stylishjm
Posts: 10
Joined: 10 Feb 2012 03:34

Re: Adding Headers to a csv file?

#3 Post by stylishjm » 14 Feb 2012 04:02

Possibly a regular task, set up using windows scheduled tasks.

This file will sit on 150 computers so installing extra software is a no-go to be honest.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Adding Headers to a csv file?

#4 Post by foxidrive » 14 Feb 2012 04:02

I was editing as you replied. See above.

Some characters could be a problem in fieldnames. Alphanumeric text is fine.

stylishjm
Posts: 10
Joined: 10 Feb 2012 03:34

Re: Adding Headers to a csv file?

#5 Post by stylishjm » 14 Feb 2012 04:22

foxidrive wrote:I was editing as you replied. See above.

Some characters could be a problem in fieldnames. Alphanumeric text is fine.


Thank you, will try that now.

stylishjm
Posts: 10
Joined: 10 Feb 2012 03:34

Re: Adding Headers to a csv file?

#6 Post by stylishjm » 14 Feb 2012 04:39

foxidrive wrote:I was editing as you replied. See above.

Some characters could be a problem in fieldnames. Alphanumeric text is fine.


Worked perfectly, thank you!

Post Reply