if defined over variable with spaces
Posted: 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 ):
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:
It obviously checks if sim exiss but does not executes salabim and does not produce error.
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.