1

I'm writing a simple batch file for homework, it's just supposed to check if two variables are defined or not. I had it working fine, then I realized it needed to check two variables, not one. It still works but it gets a syntax error. anyone have any idea why?

:: 04/09/18
:: checkVars.bat
:: Checks if a variable name is defined


if %1 == "" (
  echo "Usage: %0 varname1"
  exit /b
) else (
  goto check
)


:check
if defined %1 (
    echo "%1 is defined"
) else (
    echo "%1 is not defined"    
)

shift /1
goto check
0

3 Answers 3

1

Or you could use a For loop:

Rem 04/09/18
Rem checkVars.bat
Rem Checks if variable names are defined
@Echo Off
If "%~1"=="" (Echo Usage: "%~0" "varname1" "varname2" etc. & Exit /B)
For %%A In (%*) Do If Defined %%~A (Echo "%%~A is defined"
) Else Echo "%%A is not defined"
Pause

You'll probably only need to surround varname1, varname2 etc. with double quotes should they contain spaces, (but it's always good practice).

Sign up to request clarification or add additional context in comments.

Comments

1

Try like this.The error occurs when there are no command line arguments and first if is interpreted like if == "" - which is a wrong syntax:

:: 04/09/18
:: checkVars.bat
:: Checks if a variable name is defined
@echo off
setlocal enableDelayedExpansion
if "%~1" == "" (
  echo Usage: %0 varname1 [varname2 [..]]
  exit /b
) else (
  goto check
)


:check
set "arg=%~1"
if not "%~1" equ "" (
    if defined !arg! (
        echo "'%~1' is defined"
    ) else (
        echo "'%~1' is not defined"

    )
) else (
    goto :endcheck
)

shift /1
goto check
:endcheck

You'll have the same problem with the if defined too.To make it more robust you can use delayed expansion and you'll need a way to exit the checking otherwise you'll hit a never ending loop.Mind that using goto will hit the performance of the script.

Comments

0

Suppose you supply just one argument, var1.

The code will check "%1" which will be var1, but then it executes shift which replaces each %n with %n+1 - and with just one argument, %2 was empty, so now %1 is empty, so your code will return to :check but attempt to execute if defined ( (since %1 is now empty) - and that's a syntax-error.

The easy way to fix your existing code is

:: 04/09/18
:: checkVars.bat
:: Checks if a variable name is defined


if "%1" == "" (
  echo "Usage: %0 varname1"
  exit /b
) else (
  goto check
)


:check
if defined %1 (
    echo "%1 is defined"
) else (
    echo "%1 is not defined"    
)

shift /1
if "%1" == "" (
  echo "Finished"
  exit /b
)
goto check

so that looping back to check will only occur if %1 exists

Note : In both cases of this line, use

if "%1" == "" (

not

if %1 == "" (

as the == operator is very literal. The strings on both sides must be identical to achieve equivalence.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.