Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bat script - how to set a variable = content of the file

Status
Not open for further replies.

pho01

Programmer
Mar 17, 2003
218
US
Hi,

Does anybody know a way in windows batch script to set a variable = content of a file, or set a variable = output of a command?

in unix, i can simply do this:
file_content=`cat filename.txt`

do you know the equivalent way to do this in windows using the batch script?
Thanks!
 
I can't think of a way. Why do you want to do this? Perhaps we can come up with a workaround.
 
I need to parse the output of a command to a monitor command that sends back the result to the monitor tool. The output contains multiple lines, so i thought of putting in in a file and assign avariable to it and parse it back. It can be done differently, but the main thing is i need to capture multiple lines and parse it back.

I tried something like below to get the output of the command, but unfortunately it can't handle multiple lines of string and assign all to a variable.

For /F "Skip=2 Tokens=*" %I in ('command') Do Set sumlist=%I

sumlist only gets the last line of the output, do you know a way to assign mutiple lines to a variable?

basically, another command to send the output back to the monitor tool is:
command monitor=1 <msg=sumlist>
where msg can be multiple lines message to pass back.

Thanks!
 
In DOS you can do the following;

set var1=dir/s (as an example)

Then in a batch fle (as an example)

%var1% > output.txt

This will output the result of dir/s to the text file output.txt
 
Thanks itsp1965 for the response.
But it's not what I'm looking for.

Basically, what i need to do is to run a monitor command and parse the output of another command (contain multiple lines) as the parameter of the monitor command.

mon_command monitor=1 <msg=sumlist>

where sumlist is out of another command that contains multiple lines.
 
seems that something like this will work:

firstcmd | parsecmd | monitorcmd

those are pipe characters separating them. The pipes say 'take the output from my left, and pass it as input to my right'

The tricky bit is the <parsecmd> bit which I don't know exactly what it's doing, because I don't know what data it's getting, or what the output is supposed to be. If <parsecmd> is an .EXE file, then this should work.

It's easy enough to write a simple parsing console program in Delphi (I could do it for you), if what you need cannot be accomplished with one or more of the few DOS commands that can work over multiple input lines. eg. FIND would work because it takes multiple lines and only outputs those that match criteria.
 
oh wait - missed the last bit of your post where the input to monitorcmd must be as one of it's arguments. What I just said won't be suitable.

At this point if I were you, I'd write a Delphi console program to do the work if getting the output from firstcmd, parsing it, and then calling monitorcmd with the correct arguments.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top