0

I am trying to use IF statement in the below code.

@echo off
SET a=
SET b=HELLO WORLD
echo %b%
if [%a%] == [] echo "String A is empty"
if [%b%] == [] echo "String B is empty" 

I am getting the output as below

HELLO WORLD
"String A is empty"
WORLD] was unexpected at this time.

Why is WORLD] was unexpected at this time. appearing in the output. Can you please point out the error? Thanks in advance.

4 Answers 4

2

Read Using "Double Quotes"

If a single parameter contains spaces, you can still pass it as one item by surrounding in "quotes" - this works well for long filenames.

The same is valid for any command token:

if [HELLO WORLD] == [] echo "String B is empty" 
         ↑↑ command line parser expects compare operator here like == EQU GTR etc.

Use

if "%a%" == "" echo "String A is empty"
if "%b%" == "" echo "String B is empty" 
Sign up to request clarification or add additional context in comments.

Comments

1
@echo off
SET a=
SET b=HELLO WORLD
echo %b%
if "%a%" == "" echo "String A is empty"
if "%b%" == "" echo "String B is empty" 

This is because the space in %b% and is taken as two arguments to if.Quotes can be used to pass arguments with delimiters but not []

Comments

1

A better solution if all you want to know is "is the variable empty" is

if defined a (echo a is defined) else (echo a is not defined)

naturally, if not defined ... is also available.

Comments

0

Try this:

{ @echo off SET a= SET b=HELLO WORLD echo %b% if %a%== (what you want a to be) echo "String A is empty" if %b%== "HELLO WORLD" echo "String B is empty"

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.