A Windows 98 & ME forum. Win98banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Win98banter forum » Windows 98 » Software & Applications
Site Map Home Authors List Search Today's Posts Mark Forums Read Web Partners

batch programming



 
 
Thread Tools Display Modes
  #1  
Old January 10th 05, 02:18 PM
Tobias Goller
external usenet poster
 
Posts: n/a
Default batch programming

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


  #2  
Old January 10th 05, 03:02 PM
Mikhail Zhilin
external usenet poster
 
Posts: n/a
Default

Is the command, that creates a variable, a batch file, too?
If yes -- you have to run it in the same Command.com process: the parent
process (which calls your second batch) does not inherit the variables
of the child (second batch) process, if the child process is launching
via CALL statement, i.e. in the other Command.com process.

So it can be something like:

::---Main batch (main.bat) begin--
if not "%RETCODE%"="" goto CONTIN
more initial codes here
child.bat
:CONTIN
set RETCODE=
the returned X variable is using here, something like:
if "%X%"="2" then echo "Monday"
more codes here

::---Main batch (main.bat) end--

::---Child batch (child.bat) begin--
more codes here, something like:
echo.|date|find /i "Mon"
if not errorlevel 1 SET X=2
more codes here
SET RETCODE=1
MAIN.BAT

::---Child batch (child.bat) end--

Non-empty RETCODE variable says to MAIN.BAT, that it is the return from
CHILD.BAT -- but not the initial run of MAIN.BAT.
--
Mikhail Zhilin
http://www.aha.ru/~mwz
Sorry, no technical support by e-mail.
Please reply to the newsgroups only.
======
On Mon, 10 Jan 2005 14:18:52 +0100, "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


  #3  
Old January 24th 05, 05:06 PM
Tobias Goller
external usenet poster
 
Posts: n/a
Default

Greetings,

I guess my problem is a bit more tedious.
To make things short i need to set an env variable to the output of a batch
or command file

Ie.: set MYVAR = mybatch.bat

Is this possible in Win98?

Thank You




"Mikhail Zhilin" wrote in message
...
Is the command, that creates a variable, a batch file, too?
If yes -- you have to run it in the same Command.com process: the parent
process (which calls your second batch) does not inherit the variables
of the child (second batch) process, if the child process is launching
via CALL statement, i.e. in the other Command.com process.

So it can be something like:

::---Main batch (main.bat) begin--
if not "%RETCODE%"="" goto CONTIN
more initial codes here
child.bat
:CONTIN
set RETCODE=
the returned X variable is using here, something like:
if "%X%"="2" then echo "Monday"
more codes here

::---Main batch (main.bat) end--

::---Child batch (child.bat) begin--
more codes here, something like:
echo.|date|find /i "Mon"
if not errorlevel 1 SET X=2
more codes here
SET RETCODE=1
MAIN.BAT

::---Child batch (child.bat) end--

Non-empty RETCODE variable says to MAIN.BAT, that it is the return from
CHILD.BAT -- but not the initial run of MAIN.BAT.
--
Mikhail Zhilin
http://www.aha.ru/~mwz
Sorry, no technical support by e-mail.
Please reply to the newsgroups only.
======
On Mon, 10 Jan 2005 14:18:52 +0100, "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




  #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



 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
World's First Book on Professional Data Recovery Programming Author Tarun Tyagi General 0 November 26th 04 06:20 PM
dos programming barry martin General 0 September 20th 04 02:32 AM
dos batch files yousef General 0 August 7th 04 07:52 PM
dos batch files yousef General 0 August 7th 04 07:52 PM
DOS Batch file controlled my PC! Coolwater0009 Improving Performance 1 May 20th 04 06:09 AM


All times are GMT +1. The time now is 01:36 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Win98banter.
The comments are property of their posters.