You are not logged in.

#1 23 May 2013 13:32

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 446

if issues and workarounds

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

#2 23 May 2013 14:04

jeb
Member
From: Germany
Registered: 19 Nov 2010
Posts: 109

Re: if issues and workarounds

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

#3 23 May 2013 16:39

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 446

Re: if issues and workarounds

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

#4 23 May 2013 19:06

jeb
Member
From: Germany
Registered: 19 Nov 2010
Posts: 109

Re: if issues and workarounds

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. sad

Offline

#5 24 May 2013 04:06

dbenham
Member
From: U.S. east coast
Registered: 15 Apr 2012
Posts: 111

Re: if issues and workarounds

I think jeb is referring to this thread smile
http://www.dostips.com/forum/viewtopic.php?f=3&t=4246


Dave Benham

Offline

Board footer

Powered by