Assign each line of a text file to a variable

January 17, 2011 at 02:22:53
Specs: Windows XP

I have what should hopefully be a very simple scenario.

I have a batch file which relies on a text file to retrieve a set of values, line by line. Each line represents a server name. To illustrate, let's say the servers are listed as follows:

WebServer 1
AppServer 2
DatabaseServer 3

For each server name listed in the text file, the batch script should assign it to a unique variable and then perform some other process (e.g. deploy some files), as required for that particular server.

I believe the For Loop will be best suited for this particular requirement and would be extremely grateful if anyone could assist.

Thank you.

See More: Assign each line of a text file to a variable

January 17, 2011 at 13:33:32
Try this, the variables will be able to be accessed through "!Var1!" to the max var you have.

SETLOCAL EnableDelayedExpansion

SET Num=1
FOR /f "tokens=*" %%a IN (2.txt) DO (
SET Var!Num!=%%a
SET /a Num+=1

ECHO !Var1!
ECHO !Var2!

Can I put a question here?

Report •

January 17, 2011 at 14:42:53
Hi Matt123,

Thanks ever so much for coming to my rescue. The content of my input file is read successfully by the script. Thanks.

The subsequent processing of these input file values continues to stump me though and I'd be grateful if you could assist. Basically my batch file should retrieve each server name as described in my original post and uses it to replace an existing server name in a web config file.

For e.g, if the web config file has the following keys:

<arg key="MyApp1Root" value="http://localhost:9005/config"/>
I would like to replace the word localhost with "WebServer 1",

<arg key="MyApp2Root" value="http://localhost:9012/config"/>
I would like to replace the word localhost with "AppsServer 2"

And finally for:
<arg key="WWWRoot" value="http://DBServer:9012/config"/>
I would like to replace DBServer with "DatabaseServer 3"

Any ideas how I can achieve this please?


Report •

January 17, 2011 at 16:35:53
First of all, there is no apparent relationship between "localhost" and "WebServer 1", and "localhost" and "AppsServer 1".

Also, batch files aren't good at processing things like this. All the special characters throw it off, and it really doesn't turn out well.

I know some members here are well acquainted with other types of scripting (Awk, VBS maybe) that might get the job done. If they are able to help, they will.

Can I put a question here?

Report •
Related Solutions

Ask Question