0

I using multiple variables in a batch file, I need to check if they contains any value.

Currently I am using:-

IF [%TEST%] == [] (
echo The Value of Test is empty!
exit /b )

But using this is increasing the lines of code, is there a some way like OR in batch that can be used (like we do for shell).

2
  • A variable cannot have no value, it is either defined, (i.e. it has a value), or it is not defined. Example: If Not Defined TEST Echo TEST is not defined. In the example above by Gerhard, you don't need an if, because only defined variables would be passed from the For set, in the parentheses. Commented Sep 11, 2018 at 7:44
  • How about a for loop? for %%i in (%var1% %var2% %var3%) do echo %%i to only echo variables that are defined, as mentioned by @Compo . I deleted original comment to repost and remove if Commented Sep 11, 2018 at 7:56

2 Answers 2

4

I think the easiest would be to simply create a for loop and test each value. So consider the following:

If you want to do nothing if a value is empty (undefined), and simply use the variable's value, which are defined:

@echo off
set "test1=Value1"
set "test2=Value2"
set "test3="
set "test4=Value4"
set "myvars=%test1% %test2% %test3% %test4%"

for %%i in (%myvars%) do echo %%i

If you actually want to only test if a variable is undefined or not:

@echo off
set "test1=Value1"
set "test2=Value2"
set "test3="
set "test4=Value4"
set "myvars=test1 test2 test3 test4"

for %%i in (%myvars%) do if not defined %%i (echo %%i not defined) else echo %%i Defined

or if you simply want to rather include the variables in the loop and not assign them to a single variable:

@echo off
set "test1=Value1"
set "test2=Value2"
set "test3="
set "test4=Value4"

for %%i in (test1 test2 test3 test4) do if not defined %%i (echo %%i not defined) else echo %%i Defined
Sign up to request clarification or add additional context in comments.

2 Comments

The first example simply prints test1, newline, test2 newline, etc. You're not actually getting the values, only the names.
@DodgyCodeException Thanks, forgot to test it. fixed.
4

I think the simplest way is this:

if "%test1%%test2%%test3%" equ "" ( 
   echo The three variables are empty!
   exit /b
)

EDIT: Additional tests added

Ok. The code below check that at least one of the variables is not empty:

if "%test1%%test2%%test3%" neq "" ( 
   echo At least one variable is not empty!
   exit /b
)

And the next code check that at least one of the variables is empty:

set "allVars=|%test1%|%test2%|%test3%|"
if "%allvars:||=%" neq "%allVars%" (
    echo At least one variable is empty
)

In the last code it is necessary to select a delimiter character that can not appear in any variable.

4 Comments

This will only test if all 3 variables are empty, not only one? OP specifically specifies OR.
@GerhardBarnard - add an ELSE and you can also take action if at least one variable is defined (not empty), which may be all the OP needs. Impossible to tell because the original question is vague on the specific goal.
@dbenham I agree yes, but op says like using an OR, so he wants if test1 or if test or... so in this case, else will do yes, but it is not included, hence my comment to Aacini to perhaps clarify :)
@GerhardBarnard - Strike my previous comment. The OP most likely needs to know if at least one variable is undefined, which cannot be determined with this technique.

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.