Page 1 of 1

if defined over variable with spaces

Posted: 29 Dec 2014 05:45
by npocmaka_
While variable names with other special symbols can be escaped this not work with spaces.
The only I've found to make if defined works in this case is with second variable and delayed expansion (I know the output can be processed with for /f ):

Code: Select all

@echo off

setlocal enableDelayedExpansion

set sim salabim=magic
set sim=simulation

set "checker=sim salabim"

if defined !checker! echo #

rem the commented lines  produce errors
rem if defined sim salabim echo $
rem if defined %checker% echo @
rem if defined "sim salabim" echo *


endlocal




The thing which bothers me if there'd variable with a name as a first word of the variable with the spaces and the space is escaped with ^ the example works:

Code: Select all

setlocal disableDelayedExpansion
set sim salabim=magic

rem this produces error
rem if defined sim^ salabim echo -

set sim=simulation

set "checker=sim salabim"

rem why this works ???
if defined sim^ salabim echo -

endlocal


It obviously checks if sim exiss but does not executes salabim and does not produce error.

Re: if defined over variable with spaces

Posted: 29 Dec 2014 06:24
by penpen
I assume it is a bug in the if implementation:
Parsing the command line is correct, but the "if" command ends the detection of the environment variables name at the first whitespace.

You could also use for, to check if this environment variable is defined (tested under win xp):

Code: Select all

@echo off
setlocal disableDelayedExpansion
set sim salabim=magic

for %%a in ("sim salabim") do if defined %%~a echo ---
endlocal
goto :eof

penpen

Re: if defined over variable with spaces

Posted: 30 Dec 2014 02:59
by npocmaka_
penpen wrote:I assume it is a bug in the if implementation:
Parsing the command line is correct, but the "if" command ends the detection of the environment variables name at the first whitespace.

You could also use for, to check if this environment variable is defined (tested under win xp):

Code: Select all

@echo off
setlocal disableDelayedExpansion
set sim salabim=magic

for %%a in ("sim salabim") do if defined %%~a echo ---
endlocal
goto :eof

penpen



Aah.Yes.I did not think about this way.
Definitely there's something buggy in in if defined

Code: Select all

@echo off

setlocal

   set "undefined1="
   set "undefined2="
   set "undefined3="

   set "var1=1"

   if defined var1^ undefined1^ undefined2^ undefined3 echo ###

endlocal

Re: if defined over variable with spaces

Posted: 30 Dec 2014 11:24
by Liviu
penpen wrote:Parsing the command line is correct, but the "if" command ends the detection of the environment variables name at the first whitespace.

This looks to be the case, indeed, except it's not just whitespace, but any delimiter. The same happens with "if defined var^,etc", "if defined var^;etc", "if defined var^=etc".

Liviu

Re: if defined over variable with spaces

Posted: 30 Dec 2014 17:09
by penpen
I've just found out that the "if defined" command is able to handle delimiters as part of a variable name:

Code: Select all

@echo off
setlocal
set ^""test ;&|,"=a^"
if defined "test ;&|," echo ok
endlocal
goto :eof
So my next theory is, that the "if defined" command doesn't know the escaped state of the delimiter (for whatever reason).

penpen