Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 833
susil95 Avatar asked Sep 24 '16 17:09

susil95


2 Answers

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
like image 200
Hackoo Avatar answered Sep 24 '22 22:09

Hackoo


@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
like image 20
user10865636 Avatar answered Sep 24 '22 22:09

user10865636