Batch file to rename .csv field

Dell / Who cares
June 3, 2009 at 10:01:14
Specs: Windows XP, 4gb
Hi,

I need to rename a header field in a .csv file
before uploading it via winscp.com to a server.

Is that possible?

Thanks, great site, always inspiring.


See More: Batch file to rename .csv field

Report •


#1
June 3, 2009 at 12:09:10
@echo off & setLocal EnableDelayedExpansion

echo my, new, header > new.csv

for /f "skip=1 tokens=* delims=" %%a in (old.csv) do (
echo %%a >> new.csv
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
June 3, 2009 at 13:21:36
Thanks M2 - you're DaMan! Except I don't get it....Duh-Op!

Here's a sample .csv: if I want to change row 1B's field name from "fname" to "first" where do I call that out in the batch file?

Sir,fname,CWID,place,title,lname,123,Country,Email
Mr.,Joe,122,Pond,Sweet,Dude,556,us,dog@dude.net
Mrs.,Joesse,123,Pond1,Sweet1,Dude1,556,us,dog1@dude.n
et
Ms.,Josaphine,124,Pond2,Sweet2,Dude2,556,us,dog2@dude.
net

More importantly, I understand basic .bats from my ol DOS
days MD,CD,REN etc, But where can I go to really learn it. To
understand "setLocal EnableDelayedExpansion", "for /f
"skip=1 tokens=* delims=" %%a" and other cmds?

Thanks,
Lou


Report •

#3
June 3, 2009 at 13:39:49
Also...I don't want to copy the text to the .bat file, the real db
is 66mb big. I'm hoping there's a way to set up a variable or
something to call 1B then ren it.

~~~~~~~~~~~~~~~~~

M2,

You solved a similar problem, but I only need to rename a field, not move data: Check out

Name: majinfaisal
Date: September 9, 2007 at 14:06:01 Pacific
Subject: Batch needed to add column to CSV

http://www.computing.net/answers/pr...

I didn't see the final solution.

Txs, Lou


Report •

Related Solutions

#4
June 3, 2009 at 15:31:26
SWEET! I used your code from sept07 and modified it but I
need help debugging it. It renamed 1i "Address" to "Email"
but it did not complete the data transfer...see below:

~~~~~~~~~~~~~~~~~~~~~~~ new.csv

Sir, fname, CWID, place, title, lname, 123, Country, Email
Mr., Joe, 122, e, g, i , Pond
Mrs., Joesse, 123, e, g, i , Pond1
Ms., Josaphine, 124, e, g, i , Pond2
Sir, Joe, 125, e, g, i , Pond3
Mr., Joe, 126, e, g, i , Pond4

~~~~~~~~~~~~~~~~~~~ old.csv
Sir,fname,CWID,place,title,lname,123,Country,Email
Mr.,Joe,122,Pond,Sweet,Dude,556,us,dog@dude.net
Mrs.,Joesse,123,Pond1,Sweet1,Dude1,556,us,dog1@dude.n
et
Ms.,Josaphine,124,Pond2,Sweet2,Dude2,556,us,dog2@dude.
net
Sir,Joe,125,Pond3,Sweet3,Dude3,556,us,dog3@dude.net
Mr.,Joe,126,Pond4,Sweet4,Dude4,556,us,dog4@dude.net

~~~~~~~~~~~~~~~~~~~~~ ren-csvfield.bat
@echo off
setLocal EnableDelayedExpansion
for /f "tokens=1-4 delims=, " %%a in (old.csv) do (
set /a N+=1
if !N! equ 1 (echo Sir, fname, CWID, place, title, lname, 123, Country, Email > new.csv
) else (
for /f "tokens=1-2 delims=." %%P in ('echo %%d') do (
set st=%%Q & set hn=%%P
)
echo %%a, %%b, %%c, %d, %e, %f, %g, %h, %i !st!, !hn! >> new.csv
)
)

~~~~~~~~~~~~~~~~~~~~~ sept07, batch-file-to-rename-csv-field/19279.html
~~~~~~~~~~~~~~~~~~~~~~~

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=1-4 delims=, " %%a in (my.csv) do (
set /a N+=1
if !N! equ 1 (echo id, first_name, second_name, location, housen > new.csv
) else (
for /f "tokens=1-2 delims=." %%P in ('echo %%d') do (
set st=%%Q & set hn=%%P
)
echo %%a, %%b, %%c, !st!, !hn! >> new.csv
)
)

Txs M2!


Report •

#5
June 3, 2009 at 15:56:59
I'm so close. Here's the modified .bat

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=1-9 delims=, " %%a in (old.csv) do (
set /a N+=1
if !N! equ 1 (echo Sir, fname, CWID, place, title, lname, 123,
Country, Email > new.csv
)
echo %%a, %%b, %%c, %%d, %%e, %%f, %%g, %%h,
%%i, >> new.csv
)
)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ new.csv
It does the rename, but adds it as row 1:

Sir, fname, CWID, place, title, lname, 123, Country, Email
Sir, fname, CWID, place, title, lname, 123, Country, Address,
Mr., Joe, 122, Pond, Sweet, Dude, 556, us, dog@dude.net,
Mrs., Joesse, 123, Pond1, Sweet1, Dude1, 556, us,
dog1@dude.net,
Ms., Josaphine, 124, Pond2, Sweet2, Dude2, 556, us,
dog2@dude.net,
Sir, Joe, 125, Pond3, Sweet3, Dude3, 556, us,
dog3@dude.net,
Mr., Joe, 126, Pond4, Sweet4, Dude4, 556, us,
dog4@dude.net,


Report •

#6
June 4, 2009 at 09:25:45
M2 Thanks for letting me play along! Now that I jumped
through hoops I finally realized that your original reply IS
Correct and works beautifully.

~~~~~~~~~~~~~~~~~~~~~~ Your Solution, Edited!

@echo off & setLocal EnableDelayedExpansion
echo Mister, fname, CWID, place, title, lname, 123, Country,
Email > new.csv
for /f "skip=1 tokens=* delims=" %%a in (old.csv) do (
echo %%a >> new.csv
)

~~~~~~~~~~~~~~~~~~~~~~~~~~~ Result Sir renamed to
Mister and Address renamed to Email!

Mister, fname, CWID, place, title, lname, 123, Country, Email
Mr.,Joe,122,Pond,Sweet,Dude,556,us,dog@dude.net
Mrs.,Joesse,123,Pond1,Sweet1,Dude1,556,us,dog1@dude.n
et
Ms.,Josaphine,124,Pond2,Sweet2,Dude2,556,us,dog2@dude.
net
Sir,Joe,125,Pond3,Sweet3,Dude3,556,us,dog3@dude.net
Mr.,Joe,126,Pond4,Sweet4,Dude4,556,us,dog4@dude.net

I am humbled but excited, I found ss64.com which helped me
understand the code!

I thank you so much, I really am reborn...I had no idea
cmd.exe was so powerful!


Report •


Ask Question