1.

Solve : Batch file to rename a header field in a .csv file?

Answer»

I need to learn how to set up a .bat to change a header field name in a .csv file.

My batch file experience is limited to basic DOS cmds but I've seen some amazing stuff done with variables and "setLocal EnableDelayedExpansion" which I wish I understood how to do myself, haven't found a goo site to learn it on yet...

Txs!how does your file look like and what are you changing..describe CLEARLY with examples!Change the header? Fairly straightforward!

echo off
setlocal enabledelayedexpansion
set num=0
for /f "delims=" %%A in ('type File.csv') do (
set /a num+=1
if !num!==1 (echo Newheader > temp.txt) else (echo %%A >> temp.txt)
)
type temp.txt > File.csv
del temp.txtmore straightforward way in XP
Code: [Select]echo newheader >>temp
more +2  file >> temp
ren temp file
Quote from: gh0std0g74 on June 05, 2009, 05:39:06 PM

more straightforward way in XP
Code: [Select]echo newheader >>temp
more +2  file >> temp
ren temp file

I don't think that my solution wouldn't work in any OS other than XP...and I recently learned how to use FOR (not PERFECTLY mind you), so I was GLAD to do it with that.Since "for" didn't get any SWITCHES until at least WINDOWS 2000, your script would have difficulty working on, say, windows 98.

the "More" solution would work from DOS 3.22 upwards. Quote from: BC_Programmer on June 05, 2009, 05:55:11 PM
Since "for" didn't get any switches until at least windows 2000, your script would have difficulty working on, say, windows 98.

the "More" solution would work from DOS 3.22 upwards.
Ohh...ok...


Discussion

No Comment Found