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
+1and all other characters are removed.To begin and terminate the line in a proper way there is always one extra
+1at the beginning, which will be compensated by a-1in front.The main trick is to replace the complete text from one quote to the next with exactly one
+1by replacing each quote with!!#:#=.This works as
!#:#=...<some text>...!will always be expanded to+1, as the content of the variable#is+1and 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