Solved Need to read one line at a time from a .csv file using .bat

May 21, 2012 at 08:26:18
Specs: Windows 7 Professional x64 SP1, Variable, medium power
I'm attempting to automate something at work that requires minimal background process. Normally I'd use C#, but this is problematic, as part of what I'm automating is a test process that scans the system looking for running processes. Command windows are fine, but most anything beyond that get's to be problematic.

As part of kicking off this series of tests, I am trying to find a way to get a batch file to read one line at a time from a .csv file. Each line contains 4 values (separated by commas, of course) which should be put into variables. The first var will be the test number. When starting the batch file, I'll be providing a test number as an arg, and want it to compare the test number received to the test number it read from the file. If it matches, continue testing. If not, move on to the next line and read that data into the variables.

Everything else is really simple, but I can't figure out how to get it to read and parse a .csv file one line at a time in the manner described. Any help would be appreciated.

See More: Need to read one line at a time from a .csv file using .bat

May 21, 2012 at 10:36:43
✔ Best Answer
for /F "tokens=1-4,delims=," %%A in (test.csv) do call jock.bat %%A %%B %%C %%D

will read each line in turn from test.csv, and parse the line into 4 variables starting with A, then invoke a subshel for jock.bat passing those 4 into it.

try "help for" from the command line for fuller details.

Report •

May 21, 2012 at 10:44:49
So, in this:

for /F "tokens=1-4,delims=," %%A in (test.csv) do call jock.bat %%A %%B %%C %%D

does the batch file automatically just assign incremental letters to each token given? For instance, if it's

for /F "tokens=1-4, delims=," %%Q in (test.csv)

would I be able to use %%Q, %%R, %%S, and %%T as variables for each token later in that batch file if I had a use for them?

Thanks for the answer. That really helped get me on the right path. In case you can't tell, batch files are NOT my forte! :)

Report •

May 21, 2012 at 13:48:34
Yes, the tokens are assigned sequentially.


Report •

Related Solutions

May 21, 2012 at 14:15:27
Excellent! Got a functioning batch that appears to be smarter than I, now.

How the heck do I mark this thing as resolved, now? lol

Report •

Ask Question