Here’s a basic method of parsing a CSV file using Microsoft Batch.
The following code will allow me to use all the CSV fields. Let’s suppose the CSV contains the following:
Name, Email, Username, Password John Doe,email@example.com,jdoe123,secretpassword Jane Doe,firstname.lastname@example.org,janeybug1,topsecret
@echo off setlocal for /F usebackq tokens=* delims=, %%a in (c:tempusers.csv) do ( echo Name: %%a echo Email: %%b echo Username: %%c echo Password: %%d )
Output from Batch
Name: John Doe Email: email@example.com Username: jdoe123 Password: secretpassword Name: Jane Doe Email: firstname.lastname@example.org Username: janeybug1 Password: topsecret
[stextbox id=info caption=Note]If the CSV file you are working with uses quotes also, then when using the variable, specify
%%~a to strip the quotes from the variable. See
If you are only interested in a few columns, you could use the following for the
tokens= specifier in the
That will only return the 3rd and 4th result based on the
, delimiter and in this case it is the username and password. You still use
%%b for your variables.