Discussion Locked

Batch file scripting

0 helpful
14 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,
Much as I pride myself on my scripting skills, this one has me stumped. I have a script which installs some software, then uses a series of echo commands to create another script which performs a cleanup IE deleting the source files, the original script and finally itself. It goes like -

echo my command 1 >> cleanup.cmd
echo my command 2 >> cleanup.cmd
start cleanup.cmd

I'm sure you get the idea. Anyway, one of the lines I echo into the cleanup file goes like -

echo If ^%errorlevel^%==0 do some command >> cleanup.cmd

Note the use of the carat ^ to tell the command interpreter not to treat the % signs as reserved characters, which stops it merely echoing the current value for errorlevel into the cleanup script.
NOW,if you type

echo ^%errorlevel^%
into a command prompt you will get
%errorlevel%
echoed back at you, put it in a cmd file and you get NOTHING, nada, nicht. WHY? It's been driving me up the wall all evening. It can't be output stream related, as both streams are piped to the window in a cmd session whether running a script or typing commands. What's different then between typing in the string, and running it in a batch file? shouldn't the result be the same? what I'm after is a either an explanation of why this happens, or a technique to make the scripted echo of errorlevel work. No workarounds please, restructuring the scripts is something I can do myself - I want to know why this is happening for future reference. BTW I've tried this on win2k and XP with similar results......

14 replies

by joseph moore

Ok, so let me see if I am understanding this correctly. You are trying to export the next command the IF %ERRRORLEVEL%= will do when it =0. You don't want to export out the errorlevel iteself (the # it equals); you want to export out to a .CMD file the command the IF statement jumps to when errorlevel=0.
Do I have this right?
Ok.
Here is a sample BAT file script I had (with a few changes) that is using the IFMEMBER tool from the resource kit:

@echo off
cls
ifmember %1 >>errortest.txt
echo %errorlevel% >>errortest.txt

echo if errorlevel=1 goto one >>test2.cmd
if errorlevel=0 goto zero
echo NOPE >>errortest.txt
EXIT

:one
echo one is the magic number >>errortest.txt
pause
goto end

:zero
echo zero is your errorlevel >>errortest.txt
pause

:end
echo BYE >>errortest.txt

by joseph moore

Now, when I put in a valid group that my user login belongs to, this is the output of the text file ERRORTEST.TXT:
Group CHGFFM\MIS matches list.
1
zero is your errorlevel
BYE

And here is what is in the CMD file that I have being built:if errorlevel=1 goto one

by joseph moore

Now, when I use a group I do NOT belong to, here is the output from ERRORTEST.TXT:
0
zero is your errorlevel
BYE

And the output for TEST2.CMD:
if errorlevel=1 goto one

by joseph moore

So, all of the above is messed up! First, I just figured out you are not trying to catch the output of the jump line in the CMD file; you are trying to catch the IF line in the CMD file! So, that makes a difference.
Plus, I think I have the answernow.
It looks like the echo if errorlevel=1 goto one >>test2.cmd line does NOT function as an IF statement. It is not processed, no matter how you phrase it. That is why in my output file ERRORTEST.TXT, I am always getting the :zero section of my BAT file, eventhough I am getting ERRORLEVEL=1 which should cause the condition to jump to section :one.
Well, it is not jumping to :one. Instead, it is treating %ERRORLEVEL% as =0, and jumping to the :zero section. That is why I am getting the Zeromessage.
Ok.

I tried to get this to work with the ECHO IF line spitting itself out to the CMD file AND at the same time actually using it as a normal IF statement. Just did not happen at all.
The only way I can get this to work is to duplicate the line. Here is what I have now:

@echo off
cls
ifmember %1 >>errortest.txt
echo %errorlevel% >>errortest.txt

echo if ^%errorlevel%^=1 goto one >>test2.cmd
if errorlevel=1 goto one
if errorlevel=0 goto zero
echo NOPE >>errortest.txt
EXIT

:one
echo one is the magic number >>errortest.txt
pause
goto end

:zero
echo zero is your errorlevel >>errortest.txt
pause

:end
echo BYE >>errortest.txt

by joseph moore

I now have the first line being the ECHO IF line, spitting itself out to TEST2.CMD. The next line is a normal IF using ERRORLEVEL with no % or ^ variable parameters, which is how it works in a BAT file as a variable.
This works.
I now have in ERRORTEST.TXT text file:
Group CHGFFM\MIS matches list.
1
one is the magic number
BYE

And in TEST2.CMD I have:
if 1=1 goto one

by joseph moore

So, I guess my recommendation is to handle this in two steps, one line for just the CMD file output of the command line, and then a second line for the actual script file logic. This might not be the best way, but at least it accomplishes the two steps of 1) getting the CMD file built and 2) actually having a BAT script that works!

Two last things: I did this on Win2KPro. And, yes, my NT domain is called CHGFFM.

Hope this helps out.

by joseph moore

Dang! 2 of my additional comments to my previous answer are missing!
Ok, here is what they said in summary.
I figured out you want to save the current line with ECHO IF ERRORLEVEL to the .CMD file, not the line a true condition would jump you to.
So, I figured out how to do this. The entire conditional statement does NOT work with the ECHO. It just won't execute. You can get a positive result, but it won't follow through. That is why my ERRORTEST files did not work correctly.
So, here is my sample script with my fix in it:

@echo off
cls
ifmember %1 >>errortest.txt
echo %errorlevel% >>errortest.txt

echo if ^%errorlevel%^=1 goto one >>test2.cmd
if errorlevel=1 goto one
if errorlevel=0 goto zero
echo NOPE >>errortest.txt
EXIT

:one
echo one is the magic number >>errortest.txt
pause
goto end

:zero
echo zero is your errorlevel >>errortest.txt
pause

:end
echo BYE >>errortest.txt

So, do the line twice.
echo if ^%errorlevel%^=1 goto one >>test2.cmdif errorlevel=1 goto one

The first one will spit out into the file TEST2.CMD:
if 1=1 goto one

And the second line will ACTUALLY do the logic and it jumps down to the :one section on a positive result.

I hope this is what you are looking for.
And I hope this answer posts to the thread correctly!

by beermonster

Many thanks for your efforts joseph, but I don't think you quite got what I was driving at. You say
"The first one will spit out into the file TEST2.CMD:
if 1=1 goto one "

But that's exactly what I want to avoid, I needed to echo theSTRING %errorlevel% into the second file, without having the command interpreter resolve the %errorlevel% variable to a value.

by timthetoolman

Hi BeerMonster,

Try this...

E:\Documents and Settings\Tim Medhurst>set x=errorlevel

E:\Documents and Settings\Tim Medhurst>echo %%x%%
%errorlevel%

E:\Documents and Settings\Tim Medhurst>echo %%x%% > temp.txt

E:\Documents and Settings\Tim Medhurst>type temp.txt
%errorlevel%

E:\Documents and Settings\Tim Medhurst>

Cheers,
Tim.

by timthetoolman

Hi,

...actually in a batch file the behaviour is slightly different. In fact its a little easier.

D:\downloads\Batch>type test.bat
echo %%errorlevel%%
pause
echo %%errorlevel%% > temp.txt

D:\downloads\Batch>test

D:\downloads\Batch>echo %errorlevel%
%errorlevel%

D:\downloads\Batch>pause
Press any key to continue . . .

D:\downloads\Batch>echo %errorlevel% 1>temp.txt
D:\downloads\Batch>type temp.txt
%errorlevel%

D:\downloads\Batch>

I have no idea why the behaviourshould be different...

Cheers again,
Tim.

by beermonster

Sorry abot the delay Tim, our ISP went down yesterday. Thanks for the tip, works like a dream. I sure would like to know what the difference between the command line and script processing is though...

by beermonster

Tim,
I'll be back in the office tomorrow to confirm that your suggestions work correctly in the main script, but tests on my laptop show that you are on the right track. Will award points then....

This discussion is closed to new replies.