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:
Example CSV
Name, Email, Username, Password John Doe,jdoe@example.com,jdoe123,secretpassword Jane Doe,janebug@example.com,janeybug1,topsecret
Batch Script
@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: jdoe@example.com Username: jdoe123 Password: secretpassword Name: Jane Doe Email: janebug@example.com 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 help for
[/stextbox]
If you are only interested in a few columns, you could use the following for the tokens=
specifier in the for
loop.
tokens=3,4
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 %%a
then %%b
for your variables.