Solved how to get .bat to read another file, then use as variable

October 18, 2012 at 15:54:56
Specs: Server 2003
I've got a tricky situation. I have a batch file that uses %clientname% to set the environment variable for CLIENTNODE and then launches an app (this is in a Terminal Service setup).

Basically script starts like this:
net use y: \\fileserver\share

set clientnode=%clientname%
setX clientnode %clientname%


and the script goes on to do other stuff and eventually launch an application. The reaon for this is because I'm basically emulating a PC name in the TS environment, because within that application, a "NODE" is configured and has to match a certain machine name.

This all worked great, UNTIL they stopped inputting the "NODE" within the application to match the pc name (%clientname%).

For the life of me I can't seem to figure out how make the batch file read another file, i.e. a txt file located on \\fileserver\share, and then when it finds a match for the %clientname%, use variable X or whatever.

So for example, if I had a text file that could be maintained like:

So in the example, LABTESTPC-10 is the pc name, but LBTST10 is the "NODE" name in the application.

Is there a way I can take my original .bat file and make it read the text file located on the share, and if their %clientname% is matching the first column, i.e. LABTESTPC-10, then have a variable that gets plugged into the 'setx CLIENTNODE=X, where X is obviously the second column of the same line of the text file, i.e. LBTST10.

I have spent a LOT of time trying to figure it out, but everything I attempted fell short so I thought I would just reach out to someone who is more versed with scripting to see if it's even possible to make this work. Then the enduser in charge of the app just has to maintain the text file, so when a new NODE is added to the app, they just need to fill in the pc name and then the NODE name.

Thanks all.


See More: how to get .bat to read another file, then use as variable

Report •

October 18, 2012 at 17:47:34
✔ Best Answer

::==== begin script
for /f "tokens=1,2 delims=," %%a in ('findstr /c:"%clientname%" \\server\share\list') do set x=%%b
echo is this right?===%x%===
::===== end

Report •

October 18, 2012 at 22:00:03
Thank you nbrane. I see where you're going with that, and it does make sense. However, I don't get any output, so I'm not sure how else to modify it.

echo off
for /f "tokens=1,2 delims=," %%a in ('findstr /c:"%clientname%" \\fileserver\share\NODES.txt') do set x=%%b
echo is this right?===%x%===


all i get is:
is this right?======

My NODES.txt is currently:


I just can't quite seem to figure out the tokens part in a batch file.

Report •

October 19, 2012 at 04:58:44
It looks like the tokens make sense.

Have you tried running it with echo on to see if it picks up the file?

It looks like that may be what is happening.

:: mike

Report •

Related Solutions

October 19, 2012 at 06:44:04
I just deleted my original one and retyped it, making sure I didn't fat finger anything, and it did work that time. I had also modified permissions on the shared location (I was testing with a standard user accnt), and I believe that was possibly it.

Thanks for everyone's help. My final script is obviously way more detailed, but using these for a guide has really helped me put it together and I believe it will work just fine.

Report •

Ask Question