I have a FOR /F loop in a batch file that reads CSV output from Outlook exported contacts. The first 3 columns in the CSV output a the most significant for me and all must be filled. Since there is no way to filter exported contacts I wrote a simple batchfile that reads the CSV output in a FOR /F using loop using DELIMS=, ignoring all rows that have no data in any of the first 3 columns. Some rows start with three commas (thus 3 empty columns) but the first token is then loaded with data from the fourth column. Can someone explain?
1 Answer
As has already been pointed out, the FOR /F command treats consecutive delimiters as a single delimiter. If all fields were quote surrounded this would not be a problem. Now you could in theory use two FOR /F commands to parse the CSV. The first FOR /F would assign the whole line to a single token. That token could then be assigned to an environmental variable and then you could use string substitution to quote surround all the empty fields. Then pass that variable to a second FOR /F command to process.
Another option is to use a second batch file to parse the CSV file. It is actually a hybrid batch/jscript written by Dbenham. It does all the work for you. http://www.dostips.com/forum/viewtopic.php?f=3&t=5702
@echo off
for /f "tokens=1-3 delims=," %%A in ('parseCSV ^<test1.csv') do (
echo -------------
echo(A=%%~A
echo(B=%%~B
echo(C=%%~C
)
1 Comment
for /F will replace all consecutive delimiters by a single space, so the field value becomes modified; for instance, for /F "delims=," %L in (""a,,b,c",d") do echo %L will return "a b c" rather than "a,,b,c"...