Adding a value in CSV file with double quotes for all rows

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
shaswat
Posts: 35
Joined: 26 Aug 2015 06:08

Adding a value in CSV file with double quotes for all rows

#1 Post by shaswat » 16 Sep 2015 09:13

Hi Team,

I have requirement where I need to write a batch script that will add a value in between my CSV file and save that file. That same value must be added in all the rows. Please find the same CSV files below:
Original File:

Code: Select all

"Hello","75783","Go to Zoo","35.89"
"Hayo","75663","Go to Movie","45.89"

Required Output:

Code: Select all

"Hello","75783","Go to Zoo","New Addition","35.89"
"Hayo","75663","Go to Movie","New Addition","45.89"

Thanks a lot for all your help.

Regards,
Shaswat

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Adding a value in CSV file with double quotes for all ro

#2 Post by ShadowThief » 16 Sep 2015 09:23

Code: Select all

@echo off

for /F "tokens=1-3* delims=," %%A in (data.csv) do (
   >>output.csv echo %%A,%%B,%%C,"New Addition",%%D
)

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Adding a value in CSV file with double quotes for all ro

#3 Post by Squashman » 16 Sep 2015 09:42

shaswat wrote:Hi Team,

I have requirement where I need to write a batch script that will add a value in between my CSV file and save that file. That same value must be added in all the rows. Please find the same CSV files below:
Original File:

Code: Select all

"Hello","75783","Go to Zoo","35.89"
"Hayo","75663","Go to Movie","45.89"

Required Output:

Code: Select all

"Hello","75783","Go to Zoo","New Addition","35.89"
"Hayo","75663","Go to Movie","New Addition","45.89"

Thanks a lot for all your help.

Regards,
Shaswat

I bet if you actually tried to learn and understand how the FOR /F command works in the two previous examples you were given, you would have been able to figure it out yourself.

viewtopic.php?p=42659#p42659
viewtopic.php?p=42718#p42718

shaswat
Posts: 35
Joined: 26 Aug 2015 06:08

Re: Adding a value in CSV file with double quotes for all ro

#4 Post by shaswat » 16 Sep 2015 10:53

Squashman wrote:I bet if you actually tried to learn and understand how the FOR /F command works in the two previous examples you were given, you would have been able to figure it out yourself.

viewtopic.php?p=42659#p42659
viewtopic.php?p=42718#p42718


I'm accepting that these changes must be some minor logic related to Batch Script but as I belong to some other technology and currently in learning phase of batch script and only the help of you guys are the support for me to solve my issues related to work. But I'm really sorry for the silly questions from my end. Hope that I'll be learning the scripting soon.

shaswat
Posts: 35
Joined: 26 Aug 2015 06:08

Re: Adding a value in CSV file with double quotes for all ro

#5 Post by shaswat » 20 Sep 2015 06:31

ShadowThief wrote:

Code: Select all

@echo off

for /F "tokens=1-3* delims=," %%A in (data.csv) do (
   >>output.csv echo %%A,%%B,%%C,"New Addition",%%D
)

Hi All,

The above code is not working for my below file format:

My Input file is too long:

Code: Select all

a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aaa,abb,acc,add,aee
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aaq,abx,acy,adz,aee
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aap,abq,acg,adh,aen


So if I try with this code, then it's not working for me as the token I can use is from " a to z " but my input file is having more than 26 columns so I tried something like this below. But it didn't work. Please advice what to do.

Code: Select all

@echo off
for /F "tokens=1-31* delims=," %%A in (data.csv) do (
   >>output.csv echo %%AA,%%B,%%AC,%%D
)


My desired output:

Code: Select all

aaa,b,acc,d
aaq,b,acy,d
aap,b,acg,d


But I'm getting output as:

Code: Select all

aA,b,aC,d
aA,b,aC,d
aA,b,aC,d


Please suggest the change required in the code.

Note: For all the files having less than 26 values in a row is working fine as I'm using the same code and tokens from a-z.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Adding a value in CSV file with double quotes for all ro

#6 Post by penpen » 20 Sep 2015 14:08

This may help (untested):

Code: Select all

@echo off
::             A B  C  D
for /F "tokens=2,4,27,29 delims=," %%A in (data.csv) do (
   >>output.csv echo %%C,%%A,%%D,%%B
)
Note the tokens are sorted by value before applied to variable names, so using "tokens=27,2,29,4" wouldn't change anything.

Some more detailed "for/F" description:
http://ss64.com/nt/for_f.html
http://ss64.com/nt/for_cmd.html


penpen

shaswat
Posts: 35
Joined: 26 Aug 2015 06:08

Re: Adding a value in CSV file with double quotes for all ro

#7 Post by shaswat » 21 Sep 2015 07:09

penpen wrote:This may help (untested):
[code]@echo off
:: A B C D
for /F "tokens=2,4,27,29 delims=," %%A in (data.csv) do (
>>output.csv echo %%C,%%A,%%D,%%B
)
penpen

Thanks, your code did the work.

Post Reply