Solved

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

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

Accepted Solution

by:
oBdA earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 25

Author Comment

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

Author Closing Comment

by:SStory
Comment Utility
Great!  This did exactly what I needed!
0
 
LVL 13

Expert Comment

by:TheCapedPlodder
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
windows 10 free update 45 127
Reusing a former RAID Member as a data drive 18 66
shortcuts on desktops 14 51
how to roll into all sub dirs 7 35
If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
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…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now