?
Solved

SET environment variable in batch file from the contents of a text file

Posted on 2008-06-23
6
Medium Priority
?
3,974 Views
Last Modified: 2012-06-27
x.txt contains the name of a computer

I need
b.bat
c.bat
d.bat

to get the contents of x.txt and use that as the computername
I wanted to do so via the SET command and environment variables.

SET F=TYPE x.txt
did't give me what I'd like.... is there a way to do it with SET?
Is there a better way to accomplish this from a batch file?

Thanks
0
Comment
Question by:SStory
  • 3
  • 2
6 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 1000 total points
ID: 21850576
Enter "help for" in a command window.
To process output of another command, you'll need the "for /f" function:

for /f %%a in ('type x.txt') do @echo %%%%a now contains: %%a

You can enclose several commands in round brackets:

for /f %%a in ('type x.txt') do (
  echo Processing %%a ...
  ping %%a
)

And even subroutiones are possible:

for /f %%a in ('type x.txt') do (
  set Machine=%%a
  call :process
)
:: *** Leave the script
goto :eof
:: *** Subroutine process starts here:
:process
echo Processing %Machine%
ping %Machine%
goto :eof

You might want to chekc out http://www.robvanderwoude.com/, lots of nice scripts there.
0
 
LVL 13

Expert Comment

by:TheCapedPlodder
ID: 21853746
Elaborating on the above:

for /f %%a in (x.txt) do set target=%%a & call :routine
goto end

:routine
for /f %%b in (b c d) do call %%b.bat %target%

:end


Then within each of your sub-scripts you will need to set an environment variable to %1 which will be the computername that is being passed to it by :routine.

e.g.:

set computer=%1
ping %computer%
etc.

Cheers,

TCP
0
 
LVL 25

Author Comment

by:SStory
ID: 21855070
Thanks for your reply, but this isn't really what I had in mind.
I don't have one batch calling a bunch of batches. I have a bunch of batches to do config stuff. I may only run one of them. The problem is I am about to move them from one machine to another and wanted a way to have all batches, when they first run, read a text file that only contains COMPUTERNAME
and use that to get to the correct location.  This is needed because many of the batches copy from a network location to a local machine.  So all I really need is a way to set an environment variable at the top of each batch file based upon the value in that one file and then use that throughout the batch file.

So, is there no way to do this? I know that Type Myfile.txt would output the results of myfile.txt, but how can I get that into the environment variable?

Thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Author Comment

by:SStory
ID: 21855088
I'm sorry, I didn't see the first reply... I will try it.
0
 
LVL 25

Author Closing Comment

by:SStory
ID: 31469943
Great!  This did exactly what I needed!
0
 
LVL 13

Expert Comment

by:TheCapedPlodder
ID: 21855134
The same code applies.

At the beginning of each batch file do this:

for /f %%a in (x.txt) do set computer=%%a & call :restofscript
goto end

:restofscript
blah blah
etc.

This will read the text file and set the computer environment variable for you to use throughout the rest of the script. Dead easy.

Cheers,

TCP
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question