You are not logged in.
Pages: 1
this gives and error:
@echo off
set "if_tester="
if defined if_tester if %if_tester% LSS 0 echo defined and less than 0
goto :eof
and says 0 was unexpected at this time. . While I do not expect the things after if defined to be processed the cmd does it and detects the syntax error (bcause if_tester is nothing).The same thing if I use parentheses and new lines.With strings I have no such problems.The workaround that I use is to put one 0 after %if_tester% and after the number I compare with :
@echo off
set "if_tester="
if defined if_tester if %if_tester%0 LSS 0 echo defined and less than 0
if defined if_tester if %if_tester%0 LSS 40 echo defined and less than 4
echo(
echo(
echo now variable is defined
echo(
echo(
set /a if_tester=3
if defined if_tester if %if_tester%0 LSS 0 echo defined and less than 0
if defined if_tester if %if_tester%0 LSS 40 echo defined and less than 4
if defined if_tester if %if_tester%0 GTR 0 echo defined and bigger than 0
if defined if_tester if %if_tester%0 GTR 20 echo defined and bigger than 2
echo(
echo(
echo now variable is negative
echo(
echo(
set /a if_tester=-3
if defined if_tester if %if_tester%0 LSS 0 echo defined and less than 0
if defined if_tester if %if_tester%0 GTR -40 echo defined and bigger than -4
if defined if_tester if %if_tester%0 LSS -20 echo defined and less than -2
echo(
echo(
and the output:
now variable is defined
defined and less than 4
defined and bigger than 0
defined and bigger than 2
now variable is negative
defined and less than 0
defined and bigger than -4
defined and less than -2
Offline
The parser always parses the complete blocks, independent if they are used or not.
This is necessary, as every variable can cause a complete change of the structure.
if 1 == 0 (
echo Never %var%
) ELSE (
echo always
)
Obviously only the ELSE part will be executed, but in the next sample it's quite more complicated
@echo off
set var=) ELSE ( echo Hello) ^& if 1==1 ( echo world
if 1 == 0 (
echo Never %var%
) ELSE (
echo always
)
Offline
Good that the delayed expansion helps here.Just if defined not so useful without delayed expansion and when numbers are checked.
I'm not sure if this could be a problem too:
setlocal enableDelayedExpansion
set "if_tester="
if !if_tester! EQU 0 this will be printed
endlocal
Offline
Yes it should be safe, as even if_tester is empty, it doesn't result in a syntax error.
Somewhere at dostips, dbenham posted something about delayed expansion zero and NULL/empty issues, but I can't find it now.
Offline
I think jeb is referring to this thread
http://www.dostips.com/forum/viewtopic.php?f=3&t=4246
Dave Benham
Offline
Pages: 1