General discussion
February 15, 2002 at 04:36 AM
beermonster

Batch file scripting

by beermonster . Updated 24 years, 6 months ago

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……

This discussion is locked

All Comments