Avatar of inetomaha
inetomaha

asked on 

Checking in a batch script for null variables

I have a batch script that can run either a normal or differential backup.  I have the file test.bat, which I want to be ran in conjunction with a switch.  For example, "test.bat /norm" to run a normal backup and "test.bat /diff" to run a differential backup.

Take a look at the code to see what I have.  If I REM out the first line code works well enough, if the switches are /norm or /diff they go to the right place and if there is any other switch they get the "%1" is not a valid switch." message, though obviously not using any switch makes the code error out with an unexpected GOTO.

With the code exactly as it is below it seems as if the code always assumes that the %1 is null for some reason, regardless if a switch is actually specified or not.  It always goes to the :btnull block every time.

Any thoughts?
if not defined %1 GOTO :btnull
if %1==/norm GOTO :btnorm
if %1==/diff GOTO :btdiff
 
echo "%1" is not a valid switch.
echo.
echo Accepted switches:
echo /norm = Start a Normal backup.
echo /diff = Start a Differential backup.
echo.
pause
exit
 
:btnull
echo This script must be supplied a valid switch.
echo.
echo Accepted switches:
echo /norm = Start a Normal backup.
echo /diff = Start a Differential backup.
echo.
pause
exit
 
:btnorm
set backuptype=Normal
GOTO :btend
 
:btdiff
set backuptype=Differential
GOTO :btend
 
:btend

Open in new window

Windows BatchMicrosoft DOS

Avatar of undefined
Last Comment
inetomaha

8/22/2022 - Mon