susil95
susil95

Reputation: 300

Check if a string variable is empty in batch script

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

Upvotes: 12

Views: 70803

Answers (2)

user10865636
user10865636

Reputation: 61

@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

Upvotes: 6

Hackoo
Hackoo

Reputation: 18827

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

Upvotes: 15

Related Questions