Solved

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

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

688 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