Skip to content

Instantly share code, notes, and snippets.

@childnode
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save childnode/0f6c874ad79788a86332 to your computer and use it in GitHub Desktop.
Save childnode/0f6c874ad79788a86332 to your computer and use it in GitHub Desktop.
What is the cause for empty variables in batch files if set is used in conditon? see http://stackoverflow.com/questions/6473385 for discussion
@echo off
Set delayed=%1
If "%delayed%"=="" (
Set delayed=false
)
If %delayed%==true (
SetLocal EnableDelayedExpansion
)
echo "=============================="
echo "Test with errorlevel condition"
echo "=============================="
if errorlevel 0 (
set CamelCase=Foo.Bar
echo "TestInCondition: %CamelCase% <--------- EMPTY"
IF "%CamelCase%" == "Foo.Bar" (
echo "SUCCESS it's CamelCase"
) else (
echo "FAIL it's CamelCase"
)
IF "!CamelCase!" == "Foo.Bar" (
echo "SUCCESS #2 it's CamelCase <----- SetLocal EnableDelayedExpansion enabled and ^!VARNAME^! used"
) else (
echo "FAIL #2 it's CamelCase <--------- STILL EMPTY IN SAME RUN even with, use SetLocal EnableDelayedExpansion (use true as #1 parameter for this script)"
)
echo "===================="
set nocamelcase=Foo.Bar
echo "TestInCondition small %nocamelcase% <--------- EMPTY"
IF "%nocamelcase%" == "Foo.Bar" (
echo "SUCCESS small caps works fine"
) else (
echo "FAIL small caps"
)
echo "=========="
echo "TestInCondition CAPS %NOCAMELCASE% <--------- EMPTY"
IF "%NOCAMELCASE%" == "Foo.Bar" (
echo "SUCCESS caps lock works fine"
) else (
echo "FAIL caps lock"
)
)
echo.
echo "=============================="
echo "Test with another condition"
echo "=============================="
if 1 EQU 1 (
set SimpleCamelCase=Foo.Bar
echo "TestInSimpleCondition %SimpleCamelCase% <--------- EMPTY"
IF "%SimpleCamelCase%" == "Foo.Bar" (
echo "SUCCESS it's SimpleCamelCase"
) else (
echo "FAIL it's SimpleCamelCase"
)
echo "===================="
set nosimplecamelcase=Foo.Bar
echo "TestInSimpleCondition small %nosimplecamelcase% <--------- EMPTY"
IF "%nosimplecamelcase%" == "Foo.Bar" (
echo "SUCCESS small caps works fine"
) else (
echo "FAIL small caps"
)
echo "=========="
echo "TestInSimpleCondition CAPS %NOSIMPLECAMELCASE% <--------- EMPTY"
IF "%NOSIMPLECAMELCASE%" == "Foo.Bar" (
echo "SUCCESS caps lock works fine"
) else (
echo "FAIL caps lock"
)
)
echo.
echo "=============================="
echo "Test without condition"
echo "=============================="
set WocCamelCase=Foo.Bar
echo "TestWithoutCondition %WocCamelCase%"
IF "%WocCamelCase%" == "Foo.Bar" (
echo "SUCCESS it's WocCamelCase"
) else (
echo "FAIL it's WocCamelCase"
)
echo "===================="
set nowoccamelcase=Foo.Bar
echo "TestWithOutCondition small %nowoccamelcase%"
IF "%nowoccamelcase%" == "Foo.Bar" (
echo "SUCCESS small caps works fine in outer"
) else (
echo "FAIL small caps outer"
)
echo "=========="
echo "TestWithOutCondition CAPS %NOWOCCAMELCASE%"
IF "%NOWOCCAMELCASE%" == "Foo.Bar" (
echo "SUCCESS caps lock works fine in outer"
) else (
echo "FAIL caps lock outer"
)
echo.
echo "=============================="
echo "Test with following condition"
echo "=============================="
set BeforeCamelCase=Foo.Bar
echo "TestBeforeCondition %BeforeCamelCase%"
if errorlevel 0 (
echo "TestInCondition %BeforeCamelCase%"
IF "%BeforeCamelCase%" == "Foo.Bar" (
echo "SUCCESS it's BeforeCamelCase"
) else (
echo "FAIL it's BeforeCamelCase"
)
)
echo "===================="
set nocamelcasebefore=Foo.Bar
echo "TestBeforeCondition small %nocamelcasebefore%"
IF "%nocamelcasebefore%" == "Foo.Bar" (
echo "SUCCESS small caps works fine before condition"
) else (
echo "FAIL small caps before condition"
)
echo "=========="
echo "TestBeforeCondition CAPS %NOCAMELCASEBEFORE%"
IF "%NOCAMELCASEBEFORE%" == "Foo.Bar" (
echo "SUCCESS caps lock works fine before condition"
) else (
echo "FAIL caps lock before condition"
)
if errorlevel 0 (
echo "TestInCondition small %nocamelcasebefore%"
IF "%nocamelcasebefore%" == "Foo.Bar" (
echo "SUCCESS small caps works fine if defined before condition"
) else (
echo "FAIL small caps if defined before condition"
)
echo "=========="
echo "TestInCondition CAPS %NOCAMELCASEBEFORE%"
IF "%NOCAMELCASEBEFORE%" == "Foo.Bar" (
echo "SUCCESS caps lock works fine if defined before condition"
) else (
echo "FAIL caps lock if defined before condition"
)
)
pause
"=============================="
"Test with errorlevel condition"
"=============================="
"TestInCondition: <--------- EMPTY"
"FAIL it's CamelCase"
"FAIL #2 it's CamelCase <--------- STILL EMPTY IN SAME RUN even with, use SetLocal EnableDelayedExpansion"
"===================="
"TestInCondition small <--------- EMPTY"
"FAIL small caps"
"=========="
"TestInCondition CAPS <--------- EMPTY"
"FAIL caps lock"
"=============================="
"Test with another condition"
"=============================="
"TestInSimpleCondition <--------- EMPTY"
"FAIL it's SimpleCamelCase"
"===================="
"TestInSimpleCondition small <--------- EMPTY"
"FAIL small caps"
"=========="
"TestInSimpleCondition CAPS <--------- EMPTY"
"FAIL caps lock"
"=============================="
"Test without condition"
"=============================="
"TestWithoutCondition Foo.Bar"
"SUCCESS it's WocCamelCase"
"===================="
"TestWithOutCondition small Foo.Bar"
"SUCCESS small caps works fine in outer"
"=========="
"TestWithOutCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine in outer"
"=============================="
"Test with following condition"
"=============================="
"TestBeforeCondition Foo.Bar"
"TestInCondition Foo.Bar"
"SUCCESS it's BeforeCamelCase"
"===================="
"TestBeforeCondition small Foo.Bar"
"SUCCESS small caps works fine before condition"
"=========="
"TestBeforeCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine before condition"
"TestInCondition small Foo.Bar"
"SUCCESS small caps works fine if defined before condition"
"=========="
"TestInCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine if defined before condition"
"=============================="
"Test with errorlevel condition"
"=============================="
"TestInCondition: <--------- EMPTY"
"FAIL it's CamelCase"
"SUCCESS #2 it's CamelCase <----- SetLocal EnableDelayedExpansion enabled and !VARNAME! used"
"===================="
"TestInCondition small <--------- EMPTY"
"FAIL small caps"
"=========="
"TestInCondition CAPS <--------- EMPTY"
"FAIL caps lock"
"=============================="
"Test with another condition"
"=============================="
"TestInSimpleCondition <--------- EMPTY"
"FAIL it's SimpleCamelCase"
"===================="
"TestInSimpleCondition small <--------- EMPTY"
"FAIL small caps"
"=========="
"TestInSimpleCondition CAPS <--------- EMPTY"
"FAIL caps lock"
"=============================="
"Test without condition"
"=============================="
"TestWithoutCondition Foo.Bar"
"SUCCESS it's WocCamelCase"
"===================="
"TestWithOutCondition small Foo.Bar"
"SUCCESS small caps works fine in outer"
"=========="
"TestWithOutCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine in outer"
"=============================="
"Test with following condition"
"=============================="
"TestBeforeCondition Foo.Bar"
"TestInCondition Foo.Bar"
"SUCCESS it's BeforeCamelCase"
"===================="
"TestBeforeCondition small Foo.Bar"
"SUCCESS small caps works fine before condition"
"=========="
"TestBeforeCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine before condition"
"TestInCondition small Foo.Bar"
"SUCCESS small caps works fine if defined before condition"
"=========="
"TestInCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine if defined before condition"
D:\TEST>test
"=============================="
"Test with errorlevel condition"
"=============================="
"TestInCondition: <--------- EMPTY"
"FAIL it's CamelCase"
"FAIL #2 it's CamelCase <--------- STILL EMPTY IN SAME RUN even with, use SetLocal EnableDelayedExpansion"
"===================="
"TestInCondition small <--------- EMPTY"
"FAIL small caps"
"=========="
"TestInCondition CAPS <--------- EMPTY"
"FAIL caps lock"
"=============================="
"Test with another condition"
"=============================="
"TestInSimpleCondition <--------- EMPTY"
"FAIL it's SimpleCamelCase"
"===================="
"TestInSimpleCondition small <--------- EMPTY"
"FAIL small caps"
"=========="
"TestInSimpleCondition CAPS <--------- EMPTY"
"FAIL caps lock"
"=============================="
"Test without condition"
"=============================="
"TestWithoutCondition Foo.Bar"
"SUCCESS it's WocCamelCase"
"===================="
"TestWithOutCondition small Foo.Bar"
"SUCCESS small caps works fine in outer"
"=========="
"TestWithOutCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine in outer"
"=============================="
"Test with following condition"
"=============================="
"TestBeforeCondition Foo.Bar"
"TestInCondition Foo.Bar"
"SUCCESS it's BeforeCamelCase"
"===================="
"TestBeforeCondition small Foo.Bar"
"SUCCESS small caps works fine before condition"
"=========="
"TestBeforeCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine before condition"
"TestInCondition small Foo.Bar"
"SUCCESS small caps works fine if defined before condition"
"=========="
"TestInCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine if defined before condition"
Drücken Sie eine beliebige Taste . . .
D:\TEST>test
"=============================="
"Test with errorlevel condition"
"=============================="
"TestInCondition: Foo.Bar <--------- EMPTY"
"SUCCESS it's CamelCase"
"FAIL #2 it's CamelCase <--------- STILL EMPTY IN SAME RUN even with, use SetLocal EnableDelayedExpansion"
"===================="
"TestInCondition small Foo.Bar <--------- EMPTY"
"SUCCESS small caps works fine"
"=========="
"TestInCondition CAPS Foo.Bar <--------- EMPTY"
"SUCCESS caps lock works fine"
"=============================="
"Test with another condition"
"=============================="
"TestInSimpleCondition Foo.Bar <--------- EMPTY"
"SUCCESS it's SimpleCamelCase"
"===================="
"TestInSimpleCondition small Foo.Bar <--------- EMPTY"
"SUCCESS small caps works fine"
"=========="
"TestInSimpleCondition CAPS Foo.Bar <--------- EMPTY"
"SUCCESS caps lock works fine"
"=============================="
"Test without condition"
"=============================="
"TestWithoutCondition Foo.Bar"
"SUCCESS it's WocCamelCase"
"===================="
"TestWithOutCondition small Foo.Bar"
"SUCCESS small caps works fine in outer"
"=========="
"TestWithOutCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine in outer"
"=============================="
"Test with following condition"
"=============================="
"TestBeforeCondition Foo.Bar"
"TestInCondition Foo.Bar"
"SUCCESS it's BeforeCamelCase"
"===================="
"TestBeforeCondition small Foo.Bar"
"SUCCESS small caps works fine before condition"
"=========="
"TestBeforeCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine before condition"
"TestInCondition small Foo.Bar"
"SUCCESS small caps works fine if defined before condition"
"=========="
"TestInCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine if defined before condition"
D:\TEST>test
"=============================="
"Test with errorlevel condition"
"=============================="
"TestInCondition: <--------- EMPTY"
"FAIL it's CamelCase"
"SUCCESS #2 it's CamelCase <----- SetLocal EnableDelayedExpansion enabled and !VARNAME! used"
"===================="
"TestInCondition small <--------- EMPTY"
"FAIL small caps"
"=========="
"TestInCondition CAPS <--------- EMPTY"
"FAIL caps lock"
"=============================="
"Test with another condition"
"=============================="
"TestInSimpleCondition <--------- EMPTY"
"FAIL it's SimpleCamelCase"
"===================="
"TestInSimpleCondition small <--------- EMPTY"
"FAIL small caps"
"=========="
"TestInSimpleCondition CAPS <--------- EMPTY"
"FAIL caps lock"
"=============================="
"Test without condition"
"=============================="
"TestWithoutCondition Foo.Bar"
"SUCCESS it's WocCamelCase"
"===================="
"TestWithOutCondition small Foo.Bar"
"SUCCESS small caps works fine in outer"
"=========="
"TestWithOutCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine in outer"
"=============================="
"Test with following condition"
"=============================="
"TestBeforeCondition Foo.Bar"
"TestInCondition Foo.Bar"
"SUCCESS it's BeforeCamelCase"
"===================="
"TestBeforeCondition small Foo.Bar"
"SUCCESS small caps works fine before condition"
"=========="
"TestBeforeCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine before condition"
"TestInCondition small Foo.Bar"
"SUCCESS small caps works fine if defined before condition"
"=========="
"TestInCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine if defined before condition"
Drücken Sie eine beliebige Taste . . .
D:\TEST>test
"=============================="
"Test with errorlevel condition"
"=============================="
"TestInCondition: <--------- EMPTY"
"FAIL it's CamelCase"
"SUCCESS #2 it's CamelCase <----- SetLocal EnableDelayedExpansion enabled and !VARNAME! used"
"===================="
"TestInCondition small <--------- EMPTY"
"FAIL small caps"
"=========="
"TestInCondition CAPS <--------- EMPTY"
"FAIL caps lock"
"=============================="
"Test with another condition"
"=============================="
"TestInSimpleCondition <--------- EMPTY"
"FAIL it's SimpleCamelCase"
"===================="
"TestInSimpleCondition small <--------- EMPTY"
"FAIL small caps"
"=========="
"TestInSimpleCondition CAPS <--------- EMPTY"
"FAIL caps lock"
"=============================="
"Test without condition"
"=============================="
"TestWithoutCondition Foo.Bar"
"SUCCESS it's WocCamelCase"
"===================="
"TestWithOutCondition small Foo.Bar"
"SUCCESS small caps works fine in outer"
"=========="
"TestWithOutCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine in outer"
"=============================="
"Test with following condition"
"=============================="
"TestBeforeCondition Foo.Bar"
"TestInCondition Foo.Bar"
"SUCCESS it's BeforeCamelCase"
"===================="
"TestBeforeCondition small Foo.Bar"
"SUCCESS small caps works fine before condition"
"=========="
"TestBeforeCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine before condition"
"TestInCondition small Foo.Bar"
"SUCCESS small caps works fine if defined before condition"
"=========="
"TestInCondition CAPS Foo.Bar"
"SUCCESS caps lock works fine if defined before condition"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment