Solved need to add to fields and keep format same

June 16, 2011 at 12:52:44
Specs: Windows Vista
I have an example text file below

1,1,,ONE 1,Y
2,1,2,TEN 10,Y
3,,2,TWO 2,Y

need to add quotes to each field and end up with

"1","1","","ONE 1","Y"
"2","1","2","TEN 10","Y"
"3","","2","TWO 2","Y"

I have used a dos batch script below to add a set of quotes "" to each field in the file, but when fields with spaces , , between delimiters are found in the file they are put to the end of file, but I need the fields with spaces to remain in their proper locations. Would be greatful for any help example output file below

@echo off > out.txt & setLocal enableDELAYedeXpansion
for /f "usebackq tokens=1-6 delims=," %%a in ("in.txt") do >> "out.txt" echo "%%a","%%b","%%c","%%d","%%e","%%f

current output below which puts fields with spaces to end of file not in their correct locations

"1","1","ONE 1","Y","",""
"2","1","2","TEN 10","Y",""
"3","2","TWO 2","Y","",""


See More: need to add to fields and keep format same

Report •


#1
June 16, 2011 at 13:30:24
You want a comma delimited fille. The system cannot guess and fill in missing positions,

Report •

#2
June 16, 2011 at 15:05:12
✔ Best Answer
@echo off > out.txt & setLocal enableDELAYedeXpansion
for /f "tokens=*" %%a in (in.txt) do (
    set line=%%a
    set line=!line:,=","!
    >> "out.txt" echo "!line!"
)

My "in.txt":

1,1,,ONE 1,Y
2,1,2,TEN 10,Y
3,,2,TWO 2,Y

My "out.txt":

"1","1","","ONE 1","Y"
"2","1","2","TEN 10","Y"
"3","","2","TWO 2","Y"

When your only tool is a hammer, every problem looks like a nail.

Report •

#3
June 17, 2011 at 12:33:18
Thank's orangeboy, works perfectly exactly what I wanted. Cheers!

Report •
Related Solutions


Ask Question