View Single Post
  #5  
Old April 12th 05, 10:21 PM
BobbyG
external usenet poster
 
Posts: n/a
Default

4/12/2005

Hello Tobias,
I recently had the same question you have, and I think I may have
figured out a way that you can put the result of a command into a variable in
a batch program. Below is one simple and one complex example of how to do
this. Please email me at if you have any questions.
Good luck!

Bobby G

Here is the simple example. It is a batch program that displays the time.
Copy the code that is in between the dashed lines, paste the code into a text
editor, and save the file as a .bat file. Hopefully it will work for you.

DisplayTheTime.bat
---------------------------------------------------------------------------------------------

@echo off

: Place the current time in the variable
: called currentTime.

FOR /F "usebackq" %%a IN (`time /t`) DO set currentTime=%%a


: Tell the user what the current time is.

echo It is now %currentTime%.


: Delete the environment variable currentTime.

set currentTime=


: Pause the output so the user has time to read
: the output before the window closes.

pause

---------------------------------------------------------------------------------------------


I've also been working on a batch program that will back up certain
files. One of the tests I was trying to figure out how to perform was to
test to see if a particular file was open or closed.
I did my testing on a computer with Windows XP Professional, but
hopefully you can get the same results on your computer with Windows 98.
Below is the command that I used in my batch program. The entire command
should be placed on one line.

FOR /F "usebackq" %%a IN (`openfiles /query /v ^| find /c /i
"outlook.pst"`) DO set instancesOfTheFileThatAreOpen=%%a


The FOR statement above places the output of the command

openfiles /query /v | find /c /i "outlook.pst"

into the variable %%a, which can then be assigned to another variable if
desired,
in this case %%a is being assigned to the variable
instancesOfTheFileThatAreOpen.

The carrot symbol "^" within the FOR statement would not be needed if the
command were executed by itself on the command line, but since it is within
the FOR statement, the carrot symbol is necessary because it acts as an
escape character for the pipe symbol "|". The FOR statement will not work
without the carrot symbol "^" in front of the pipe symbol "|".

Also note that

`openfiles /query /v ^| find /c /i "outlook.pst"`

begins and ends with a backwards single quote "`", not a forwards single
quote "'".
The backwards single quote "`" is on the same key as the tilde "~", near the
top left
corner of most keyboards.

Below is the actual batch file I used. Copy and paste it into a text editor
and save it as a batch file to see if it works. Please remember not to copy
the dashed lines.

BackupMyFiles.bat
---------------------------------------------------------------------------------------------

@echo off

: Writing of this batch file started on Friday, April 8, 2005
: The last update was made on Tuesday, April 12, 2005

: The line above makes sure that the individual commands
: in the batch file are not displayed on the screen unless
: the echo command is used. The "@" character makes sure
: that the command "echo off" is not displayed also.


: Check to see if there are any instances of the "Outlook.pst" file which
are open.
: Below are some things to note about the FOR statement.
:
: 1) To use the openfiles command on your local computer you must first run
the
: command shown below in a command prompt window.
:
: openfiles /local on
:
: 2) Next, you must shut down and then start your computer back up in order
for
: the openfiles command that you just ran to take effect. You should now
be
: able to use the openfiles command to find out which files are open on
your
: computer.
:
: 3) The "usebackq" option allows you to use double quotes around
outlook.pst.
:
: 4) Note that the statement
:
: `openfiles /query /v ^| find /c /i "outlook.pst"`
:
: starts and ends with a backwards single quote "`", not a forwards
single quote "'".
: The backwards single quote is on the same key as the tilde "~" near the
top left
: corner of most keyboards. The forwards single quote is on the same key
as the
: double quote """, which is near the Enter key near the right side of
most keyboards.
:
: 5) The carrot symbol "^" must be placed before the pipe "|" symbol in
order for
: everything to work properly. The carrot symbol "^" acts as an escape
character
: for the pipe symbol "|" since the pipe symbol is a special character.
:
: 6) The purpose of the FOR statement below is to place the output of the
command
:
: openfiles /query /v | find /c /i "outlook.pst"
:
: into the variable %%a. The value of %%a can then be assigned to another
variable
: if desired, which can then be used in tests for different conditions.


FOR /F "usebackq" %%a IN (`openfiles /query /v ^| find /c /i "outlook.pst"`)
DO set instancesOfTheFileThatAreOpen=%%a



: Tell the user how many instances of the Outlook.pst file are open

echo There are %instancesOfTheFileThatAreOpen% instances of the
echo Outlook.pst file that are open.


: Output a blank line on the screen.

echo.


: If there are zero instances of the Outlook.pst file that are open,
: tell the user that the file is closed. If one or more instances
: of the Outlook.pst file are open, tell the user that the file is open.

if %instancesOfTheFileThatAreOpen% == 0 ( echo The Outlook.pst file is
closed.
) else (
echo The Outlook.pst file is open.
)


: Output a blank line on the screen.

echo.


: Delete the "instancesOfTheFileThatAreOpen" environment variable.

set instancesOfTheFileThatAreOpen=


: Pause the output of the batch program so that the user has
: time to read the output of the program before the Command Prompt
: window closes. This is necessary if the batch program is started
: by the user double clicking on the icon for the batch program.
: If the batch program was started from the command line, then the
: pause command should not be necessary since the Command Prompt
: window should stay open after the batch program has finished
: executing.

pause

---------------------------------------------------------------------------------------------

I know all this information may be confusing, but I hope it helps.

Take care,
Bobby G.


"Tobias Goller" wrote:

Hello to everybody!

How can I put a command result in a variable?

For example:

set x={output of the command hostname}

Does sombody know how to do this on win98?


Bye
Tobias Goller