Hi
Come back DOS 3.1, all is forgiven !!!
I am now using Windows XP Home edition with SP2
I need to perform a test, and depending on the result I perform one of two sets of multi-line codes, which in turn may include a further test which SHOULD affect %ERRORLEVEL%, based upon which further actions are determined.
I like the idea of "if condition ( yes code ) else ( no code )", but it kills %ERRORLEVEL% and secondary tests.
I would appreciate advice upon a more elegant construction than the ancient ugly
"if condition goto hop yes_code goto skip :hop no_code :skip"
This batch file starts by setting ERRORLEVEL to 2, and shows that the elegant 9 line structure keeps ERRORLEVEL frozen at its pre-parentheses value, after which the ugly 10 line hop/skip/goto sequence does the job I need.
@ECHO OFF
FC ALAN.TXT NONE.TXT > NUL
echo Test on NONE.TXT = %ERRORLEVEL%
IF A==A (
ECHO Delimitted by parentheses
FC ALAN.TXT ALAN_0.TXT > NUL
echo Test on ALAN_0.TXT = %ERRORLEVEL%
FC ALAN.TXT ALAN_1.TXT > NUL
echo Test on ALAN_1.TXT = %ERRORLEVEL%
) else (
ECHO A != A
)
REM The old ways work best !!!
IF NOT A==A GOTO HOP
ECHO Delimitted by GOTOs
FC ALAN.TXT ALAN_0.TXT > NUL
echo Test on ALAN_0.TXT = %ERRORLEVEL%
FC ALAN.TXT ALAN_1.TXT > NUL
echo Test on ALAN_1.TXT = %ERRORLEVEL%
GOTO SKIP
:HOP
ECHO A != A
:SKIP
PAUSE
n.b. In the above illustration I compare A==A. In practice I use a far more complicated test - but if I explained that you would probably go to sleep !!!
When I run a batch command I get the result :-
FC: cannot open NONE.TXT - No such file or folder
Test on NONE.TXT = 2
Delimitted by parentheses
Test on ALAN_0.TXT = 2
Test on ALAN_1.TXT = 2
Delimitted by GOTOs
Test on ALAN_0.TXT = 0
Test on ALAN_1.TXT = 1
Press any key to continue . . .
The correct answer is
Test on ALAN_0.TXT = 0
Test on ALAN_1.TXT = 1