?
Solved

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

Posted on 2008-06-23
6
Medium Priority
?
3,966 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

#Citrix #POC #XenDesktop #vCenter #VMware #ESX
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

752 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