- variables question
- Posted by Jay on January 26th, 2006
I've created a batch file to query a text file and make decisions based on the text file's content. The text file looks like this:
-------------------file is below------------------------------------------------------------------------------------------
-----------
0
-------------------file is above------------------------------------------------------------------------------------------
It's a 3-line file called abc.out, first line blanks, second line dashes, third line 0 preceded by blanks.
This is the content of abc.bat:
-------------------file is below------------------------------------------------------------------------------------------
for /F "skip=2 tokens=1" %%a in (abc.out) do (set abc=%%a)
echo %%abc%%
if %%abc%% == 0 goto none
if %%abc%% NEQ 0 goto some
goto error
:none
echo There are no users of ${dbase}.
goto exit
:some
echo **JOB FAILURE**. %%abc%% logins are accessing the database
goto exit
:error
echo Unknown error caused failure
goto exit
:exit
-------------------file is above------------------------------------------------------------------------------------------
The job doesn't work as expected, in that the contents of variable abc are lost immediately, so this is the output I get:
-------------------file is below------------------------------------------------------------------------------------------
C:\WINDOWS\Temp>for /F "skip=2 tokens=1" %a in (abc.out) do (set abc=%a )
C:\WINDOWS\Temp>(set abc=0 )
C:\WINDOWS\Temp>echo %abc%
%abc%
C:\WINDOWS\Temp>if %abc% == 0 goto none
C:\WINDOWS\Temp>if %abc% NEQ 0 goto some
C:\WINDOWS\Temp>echo **JOB FAILURE**. %abc% logins are accessing the database
**JOB FAILURE**. %abc% logins are accessing the database
C:\WINDOWS\Temp>goto exit
-------------------file is above------------------------------------------------------------------------------------------
Can anybody tell me why my code doesn't work, and how I can get it to work properly?
Jay
- Posted by David Candy on January 26th, 2006
You only use two %% in For loops.
--
--------------------------------------------------------------------------------------------------
Goodbye Web Diary
http://margokingston.typepad.com/har....html#comments
=================================================
"Jay" <j@b.com> wrote in message news:eijGYHpIGHA.740@TK2MSFTNGP12.phx.gbl...
I've created a batch file to query a text file and make decisions based on the text file's content. The text file looks like this:
-------------------file is below------------------------------------------------------------------------------------------
-----------
0
-------------------file is above------------------------------------------------------------------------------------------
It's a 3-line file called abc.out, first line blanks, second line dashes, third line 0 preceded by blanks.
This is the content of abc.bat:
-------------------file is below------------------------------------------------------------------------------------------
for /F "skip=2 tokens=1" %%a in (abc.out) do (set abc=%%a)
echo %%abc%%
if %%abc%% == 0 goto none
if %%abc%% NEQ 0 goto some
goto error
:none
echo There are no users of ${dbase}.
goto exit
:some
echo **JOB FAILURE**. %%abc%% logins are accessing the database
goto exit
:error
echo Unknown error caused failure
goto exit
:exit
-------------------file is above------------------------------------------------------------------------------------------
The job doesn't work as expected, in that the contents of variable abc are lost immediately, so this is the output I get:
-------------------file is below------------------------------------------------------------------------------------------
C:\WINDOWS\Temp>for /F "skip=2 tokens=1" %a in (abc.out) do (set abc=%a )
C:\WINDOWS\Temp>(set abc=0 )
C:\WINDOWS\Temp>echo %abc%
%abc%
C:\WINDOWS\Temp>if %abc% == 0 goto none
C:\WINDOWS\Temp>if %abc% NEQ 0 goto some
C:\WINDOWS\Temp>echo **JOB FAILURE**. %abc% logins are accessing the database
**JOB FAILURE**. %abc% logins are accessing the database
C:\WINDOWS\Temp>goto exit
-------------------file is above------------------------------------------------------------------------------------------
Can anybody tell me why my code doesn't work, and how I can get it to work properly?
Jay
- XP script (cmd) check several variables (Microsoft Windows) by AeS
- % variables (Microsoft Windows) by Des
- Environment Variables... (Microsoft Windows) by Steven Sinclair
- Default environment variables (Microsoft Windows) by David
- Environment Variables (Customizing) by sel

