Computing.Net > Forums > Programming > Batch file to rename .csv field

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to rename .csv field

Reply to Message Icon

Name: gr8mobile
Date: June 3, 2009 at 10:01:14 Pacific
OS: Windows XP
CPU/Ram: 4gb
Product: Dell / Who cares
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 3, 2009 at 12:09:10 Pacific
Reply:

@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


0

Response Number 2
Name: gr8mobile
Date: June 3, 2009 at 13:21:36 Pacific
Reply:

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


0

Response Number 3
Name: gr8mobile
Date: June 3, 2009 at 13:39:49 Pacific
Reply:

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


0

Response Number 4
Name: gr8mobile
Date: June 3, 2009 at 15:31:26 Pacific
Reply:

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!


0

Response Number 5
Name: gr8mobile
Date: June 3, 2009 at 15:56:59 Pacific
Reply:

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,


0

Related Posts

See More



Response Number 6
Name: gr8mobile
Date: June 4, 2009 at 09:25:45 Pacific
Reply:

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!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file to rename .csv field

Batch file to rename file(s) in dir www.computing.net/answers/programming/batch-file-to-rename-files-in-dir/13357.html

Batch file to rename file www.computing.net/answers/programming/batch-file-to-rename-file/13945.html

Batch file to rename without spaces www.computing.net/answers/programming/batch-file-to-rename-without-spaces/11415.html