I am trying to write a batch script which checks for the presence of environment variable MY_VAR and if Not set then it tries to get hold of the 1st argument passed to the script. It then removes it quotes if present (") and then removes the trailing slash (/).
I have enabled extensions via setlocal ENABLEEXTENSIONS
as my env variable MY_VAR
can have spaces in its value and hence in order to use IF with DEFINED i need to enable extensions.
However, my whole program seems to not behave properly.
@echo off
cd %~dp0
setlocal ENABLEEXTENSIONS
@REM if MY_VAR Is defined then it can have spaces
if not defined MY_VAR (
IF [%1] == [] goto :doSomething
set ARG1=%1
echo "Script is invoked with argument %ARG1%"
set ARG1=%ARG1:"=%
:doSomething
echo "I will do something"
IF [%1]==[] (
echo "Do more things1"
) ELSE (
echo "Do more things2"
)
)
else (
IF [%1]==[] (
echo "Do more things3"
) ELSE (
echo "Do more things4"
)
)
endlocal
I am invoking this as below
C:\>cmd /c call "C:\mydir\test2.bat" "c:\myDir sgds\"
The output is as below
"ARG1 is set to "
"I will do something"
"Do more things2"
Why ARG1 is NOT set properly by below code lines?
set ARG1=%1
echo "ARG1 is set to %ARG1%"