# Parse csv file to set batch script variables



## cad1llac (Nov 8, 2010)

Assume I have a csv file (eg config.csv) set out as follows:

A,B,C
1,abc,def
2,ghi,jkl

I would like to be able to pass a batch script a parameter that uniquely identifies a line (record) in the csv file, eg:

script.cmd 2

and have the script then: 

- read the line of the csv file that corresponds to the parameter given (ie 2,ghi,jkl) and 
- set variables by parsing this line, eg:

set B=ghi
set C=jkl

I am very new to any form of coding, including batch scripting. I've read the help on FOR statements and searched the net and this forum but am hoping someone might be able to point me in the right direction, help me get started or provide a solution.

Thanks.


----------



## centauricw (Jun 26, 2010)

This one is going to be hard to do regardless of how you program it because changes to the Windows Environment (a holdover from the DOS days) will only be effective for the currently running program unless you add it to the System Environment, in case it will remain in the Environment across reboots until specifically deleted (though it can be changed). The Environment is used for batch scripts, or actually Windows Command scripts, because this is the only method command scripts have for using variables.

Unless you really need to write your script as a command script, it's better to simply use this a parameter to script (as you showed in your example) and let the script get the correct values from the CSV file. VBScript or PowerShell is much better suited to something like this than a command line script.


----------



## roblooman (May 17, 2011)

i am interested in this to... is there way to "read" sections/ lines in a csv file and input those lines into variables?


----------



## Squashman (Apr 4, 2003)

About the only way to do this in batch with the input you described is if the line you need to read from is always the same line number. You can use a For loop to easily parse a comma separated file and you can then use the skip option in the For Loop to have it not read in the first two lines and start with the 3rd line.

Or if you needed lines where the first variable on each line was a specific variable you could code an IF statement inside your For Loop to only parse those lines. Without more information this is hard to know what you need to do.


----------



## roblooman (May 17, 2011)

hmmm that seems pretty strait forward do you have an example to show.....so i could see this..

thanks


----------



## Squashman (Apr 4, 2003)

roblooman said:


> hmmm that seems pretty strait forward do you have an example to show.....so i could see this..
> 
> thanks


It is not polite to hijack someones thread with your own questions. Having multiple conversations going on inside one thread gets confusing for everyone.

Start looking at some of the Batch files that have been written in the DOS category. You will see examples of just about everything.


----------

