1

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
  • 3
    A secuence of delimiters is handled as only one delimiter and delimiters at the start of the line are discarded. Commented Oct 20, 2015 at 11:48

1 Answer 1

1

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
)
Sign up to request clarification or add additional context in comments.

1 Comment

When a field is surrounded by quotes, 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"...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.