Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Set a variable from a file or a command  (Read 16864 times)

0 Members and 1 Guest are viewing this topic.

Alex McFar

  • Guest
Set a variable from a file or a command
« on: April 26, 2004, 03:09:33 AM »
I am trying to write a batch file that will set the value that a command returns to a variable.

For example

I have a command called strings which returns the first 4 letters of a string.

It is used by writing

strings left today,4

and will return toda (the first 4 letters of today)

I want to set a variable to be the value that is returned, so that I can then test it.

I have tried

%6 = (strings left today,4)

if %6 == "toda"

Another way I have tried is to output the value to a file.

strings left today,4 > output.txt

but I don't know how to read the value of output.txt into a variable.

Can someone help me do this?



PhilC

  • Guest
Re: Set a variable from a file or a command
« Reply #1 on: May 04, 2004, 08:24:33 PM »
I noticed you did not have any replies to this. This is bad because I am tring to accomplish the same thing.
Example is: set osver=`ver`
Tring to set the output of the version command into the variable osver. In unix scripting this is very simple by just doing "osver=`ver`" and the results of the ver command would be stored in the variable called osver.
  Very interested if you found a solution that works because I have already tried the approach of putting the results into a file and then no sucess in reading it back out into a variable... :-[
   Let me know if you have found a solution and I will check other places and let you know if I find something that works...

                                [email protected]

stiddy

  • Guest
Re: Set a variable from a file or a command
« Reply #2 on: May 06, 2004, 08:51:09 AM »
Hope I can help......If you can output strings data into a text file you could then us the following to assign a varible

for /f "tokens=1-2" %%a in (output.txt ) do call c:\another_batch.bat %%a

stiddy

  • Guest
Re: Set a variable from a file or a command
« Reply #3 on: May 06, 2004, 09:00:04 AM »
with the code

for /f "tokens=1-2" %%a in (output.txt ) do call c:\another_batch.bat %%a


refer to %1 in another_batch.bat