Solved

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

Posted on 2008-06-23
6
3,961 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 83

Accepted Solution

by:
oBdA earned 250 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Many people tend to confuse the function of a virus with the one of adware, this misunderstanding of the basic of what each software is and how it operates causes users and organizations to take the wrong security measures that would protect them ag…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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…

810 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