if defined over variable with spaces

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
npocmaka_
Posts: 481
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

if defined over variable with spaces

#1 Post by npocmaka_ » 29 Dec 2014 05:45

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.

penpen
Expert
Posts: 1683
Joined: 23 Jun 2013 06:15
Location: Germany

Re: if defined over variable with spaces

#2 Post by penpen » 29 Dec 2014 06:24

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

npocmaka_
Posts: 481
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: if defined over variable with spaces

#3 Post by npocmaka_ » 30 Dec 2014 02:59

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

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: if defined over variable with spaces

#4 Post by Liviu » 30 Dec 2014 11:24

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

penpen
Expert
Posts: 1683
Joined: 23 Jun 2013 06:15
Location: Germany

Re: if defined over variable with spaces

#5 Post by penpen » 30 Dec 2014 17:09

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

Post Reply