Page 1 of 1

Embedding double quotes in field values of a CSV file

Posted: 27 Aug 2015 06:28
by shaswat
Hi Team,

I am very new to batch script and don't know much about scripting. I have a requirement of embedding the values in a CSV file with double quotes.

For example: If the file looks like this : Hello,Animal,300.00,Paris
Then my output must be like this: "Hello","Animal",300.00,"Paris"

All the values except the currency value must be covered with double quotes.

My Next requirement after doing this modification is to get the selected field values only into a new CSV file.
Example:
From the current file values "Hello","Animal",300.00,"Paris"
I want the output as "Hello",300.00,"Paris"

Which means I don't want the 2nd value from the CSV file.

Any help is really appreciated. Please guys help me out, its really very urgent for me.

Thanks,
Shaswat

Re: Embedding double quotes in field values of a CSV file

Posted: 27 Aug 2015 09:55
by MattW76
Hi Shaswat. Try this:

Code: Select all

@echo off
setlocal enabledelayedexpansion
cd /d %~dp0
for /f "tokens=1,3,4 delims=," %%a in (extract.csv) do (
   set rep="%%a",%%b,"%%c"
   echo !rep!>>New.Csv
)

Re: Embedding double quotes in field values of a CSV file

Posted: 27 Aug 2015 10:20
by foxidrive
Good thinking Matt!

It can be done without delayed expansion too, as there's no need to set a variable.

Code: Select all

@echo off
cd /d "%~dp0"
for /f "tokens=1,3,4 delims=," %%a in (extract.csv) do (
   echo "%%a",%%b,"%%c">>New.Csv
)

Re: Embedding double quotes in field values of a CSV file

Posted: 27 Aug 2015 11:20
by MattW76
That was a carryover from code I first started with but figured out a better way to do it.
Thanks for the optimization!

Re: Embedding double quotes in field values of a CSV file

Posted: 28 Aug 2015 03:39
by shaswat
Thanks a lot Guys..!! @MattW76 and @foxidrive

This code is working fine for me, but I have few more questions associated with this task.

1. I am having a excel macro file with many columns in the sheet. And in few columns it is formula based so could you please suggest how can I convert the excel macro file to a csv file with the values covered with double quotes.

2. Another excel macro I have doesn't have any formulas in the sheet, and I want that excel macro to be also converted into a csv file with the values covered with double quotes.

3. Now my last question is how to select the specific columns from the converted csv file and place them in a customized order.
Example: the order of the columns is : 1,2,3,4,5,6,7,8,9
The order I want is: 6,2,1,9,6,8,3,4
and you can observe that I have skipped the 5,7 columns.

My requirement is something similar to this. Please guys help me out.

Re: Embedding double quotes in field values of a CSV file

Posted: 28 Aug 2015 07:26
by foxidrive
shaswat wrote:My requirement is something similar to this. Please guys help me out.


There are people here who are helpful, but read this first: viewtopic.php?f=3&t=6108

Re: Embedding double quotes in field values of a CSV file

Posted: 28 Aug 2015 11:02
by shaswat
Hi foxidrive,

I really appreciate the work you guys are doing. your script helped me a lot. It does work very well for me and my task is done.

But my next question was for a different assignment. Hope you have gone through my question. It was for a excel macro. If you can then please do help me.

I'm really sorry if somehow I have not followed the process.

Regards,
Shaswat

Re: Embedding double quotes in field values of a CSV file

Posted: 28 Aug 2015 11:16
by shaswat
foxidrive wrote:There are people here who are helpful, but read this first: viewtopic.php?f=3&t=6108


Thanks for sharing this with me. As I have already said, I am very new to here and I'm really sorry for the mistakes.
I have got few assignments those are inter related, so for the 1st time I have posted the question related to my 1st task, your code helped me completely and that's the reason I moved for the next question. I know my approach was not correct. Hope you understand. :(

Re: Embedding double quotes in field values of a CSV file

Posted: 28 Aug 2015 11:27
by Squashman
If you have a new unrelated question, then you should start a new thread.