This is extension to another question (Loop through CSV file with batch - Space issue)
I have csv file content like this
name,sex,age,description,date
venu,m,16,test mesg,2012-05-01
test,f,22,"He is good guy
and
brilliant",2012-05-01
I am looping this file using this command.
For /F "usebackq tokens=1-3 delims=" %%x in (test.csv) Do (
But since there is line break in second row, I am getting 3 records even though there are two records in the file.
How to fix this? Thanks in advance.
The main problem seems to be to count the quotes in a line.
If the count of quotes is odd then you need to append the next line and count again the quotes.
Counting of characters in a string is a bit tricky, if you won't iterate through all charachters.
I used here the delayed reduction technic, each quote will be effectivly replaced by a
+1
and all other characters are removed.To begin and terminate the line in a proper way there is always one extra
+1
at the beginning, which will be compensated by a-1
in front.The main trick is to replace the complete text from one quote to the next with exactly one
+1
by replacing each quote with!!#:#=
.This works as
!#:#=...<some text>...!
will always be expanded to+1
, as the content of the variable#
is+1
and so the search pattern#
can't be found.The other replacements are only necessary to avoid problems with exclamation marks and carets int the text.
And the logic for appending lines and inserting linefeeds
The Batch file below do what you want:
I added the requirements you requested in your previous topic, that is, the sex may be empty (and is changed by @ character as I explained in my answer to that topic), and the name may include commas. I tested the program with this data file:
And get these results:
Note that any field that contain commas or spaces must be enclosed in quotes.