Script 1:
Input ("Remove Quotes.cmd" "This is a Test")
@ECHO OFF
REM Set "string" variable to "first" command line parameter
SET STRING=%1
REM Remove Quotes [Only Remove Quotes if NOT Null]
IF DEFINED STRING SET STRING=%STRING:"=%
REM IF %1 [or String] is NULL GOTO MyLabel
IF NOT DEFINED STRING GOTO MyLabel
REM OR IF "." equals "." GOTO MyLabel
IF "%STRING%." == "." GOTO MyLabel
REM GOTO End of File
GOTO :EOF
:MyLabel
ECHO Welcome!
PAUSE
Output (There is none, %1 was NOT blank, empty, or NULL):
Run ("Remove Quotes.cmd") without any parameters with the above script 1
Output (%1 is blank, empty, or NULL):
Welcome!
Press any key to continue . . .
Note: If you set a variable inside an IF ( ) ELSE ( )
statement, it will not be available to DEFINED until after it exits the "IF" statement (unless "Delayed Variable Expansion" is enabled; once enabled use an exclamation mark "!" in place of the percent "%" symbol}.
For example:
Script 2:
Input ("Remove Quotes.cmd" "This is a Test")
@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET STRING=%0
IF 1==1 (
SET STRING=%1
ECHO String in IF Statement='%STRING%'
ECHO String in IF Statement [delayed expansion]='!STRING!'
)
ECHO String out of IF Statement='%STRING%'
REM Remove Quotes [Only Remove Quotes if NOT Null]
IF DEFINED STRING SET STRING=%STRING:"=%
ECHO String without Quotes=%STRING%
REM IF %1 is NULL GOTO MyLabel
IF NOT DEFINED STRING GOTO MyLabel
REM GOTO End of File
GOTO :EOF
:MyLabel
ECHO Welcome!
ENDLOCAL
PAUSE
Output:
C:\Users\Test>"C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd" "This is a Test"
String in IF Statement='"C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd"'
String in IF Statement [delayed expansion]='"This is a Test"'
String out of IF Statement='"This is a Test"'
String without Quotes=This is a Test
C:\Users\Test>
Note: It will also remove quotes from inside the string.
For Example (using script 1 or 2):
C:\Users\Test\Documents\Batch Files>"Remove Quotes.cmd" "This is "a" Test"
Output (Script 2):
String in IF Statement='"C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd"'
String in IF Statement [delayed expansion]='"This is "a" Test"'
String out of IF Statement='"This is "a" Test"'
String without Quotes=This is a Test
Execute ("Remove Quotes.cmd") without any parameters in Script 2:
Output:
Welcome!
Press any key to continue . . .
if "%1" == "" GOTO MyLabel
doesn't fatally kill the execution of the script as long as%1
has an even number of double-quotes. I see that an odd number of double-quotes in%1
kills the execution of the script with this error:The syntax of the command is incorrect.
The solution below that uses square brackets to solve the problem has been marked as the correct answer but it doesn't seem to be doing any better. That solution also fails with the same error when%1
has an odd number of double-quotes. – BroccoliIF DEFINED
only working on environment variables instead of script variables is such a waste of potential! – MetathesisCMD.bat arg1, , arg3
the following it true :%2 == %arg3
– ObcordateSETX
then you can useDEFINED
. Then they will be permanent vars. – ObcordateIF /i NOT "%var%"==""
is pretty much the only safe way to check if a script-var is defined. And that's so much more ugly thanIF DEFINED %var%
:( – Metathesis