13

I am trying to write a batch script to get a string in a variable and check if it is empty, and if it is empty then it is directed to a loop. The below code represents the problem

:loop
set /p cide=
IF NOT "a%1"=="a" (set cide="%1")
IF [%1]==[] goto loop
ELSE
echo IDE entered
TIMEOUT 5 > NUL

The program starts to loop again even if i give a string.

I tried to put IF [%cide%]==[] goto loop or IF %cide%==[] goto loop it gave an error stating "ELSE" not recognized.

Any help is appreciated. Thanks

1
  • if not defined cide goto loop Commented Sep 24, 2016 at 21:05

2 Answers 2

15

You can try something like that :

@echo off
:loop
cls & Color 0A
echo Type what you want !
set /p "cide="
IF "%cide%"=="" ( 
    Cls & Color 0C
    echo You must enter something
    Timeout /T 2 /NoBreak>nul
    goto loop
) ELSE (
    Goto Next
)

:Next
Cls
echo %cide% is entered
pause
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Hackoo, this worked. I tried the same but with different structure from what have you written, it gave either error or exits out. Very thanks man!!!
6
@echo off
:loop
cls & Color 0A
echo Type what you want !
set /p "cide="
IF [%cide%]==[] (
     Cls & Color 0C
     echo You must enter something
     choice /d y /t 2 > nul
     goto loop              
) ELSE (
     Goto Next
 )

:Next
Cls
echo %cide% is entered
pause

1 Comment

Any insight when using [] is better than quotes in the check ?

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.