Search code examples
windowsbatch-filecmdterminal

Batch file closes instead of doing GOTO command


My code is not completely completed but whilst testing it closed after I hit enter after making my selection. This should then use goto and go to :WEBSITECHECK but closes instead.

Here is my code:

echo Welcome! This program will check if a website or server is up.
echo
echo Shortcuts:
echo 'fb' for facebook.com
echo 't' for twitter.com
echo 'insta' for instagram.com
echo 'yt' for youtube.com
echo 'r' for reddit.com 

echo -------------------------------------------------------------
echo Type '1' to check a website or '2' to check an I.P. address.
echo -------------------------------------------------------------

set type=
set /p %type% = Enter selection:
 if %type%==1 GOTO WEBSITECHECK
 if %type%==2 GOTO IPCHECK

:WEBSITECHECK

set site=
set /p %site% = Enter URL or shortcut:

REM shortcuts

if %site% == fb ping facebook.com
if %site% == t ping twitter.com
if %site% == insta ping instagram.com
if %site% == yt ping youtube.com
if %site% == r ping reddit.com

if %site% !==! fb GOTO OTHERSITE
if %site% !==! t GOTO OTHERSITE
if %site% !==! insta GOTO OTHERSITE
if %site% !==! yt GOTO OTHERSITE
if %site% !==! r GOTO OTHERSITE

:OTHERSITE

ping %site%

:IPCHECK

pause

Solution

  • I have pointed out your issues in the above comment, so here's an example of how choice is used in a scenario like this.:

    @echo off & set cnt=
    setlocal enabledelayedexpansion
    choice /C 12 /M "Site or IP?:
    set "chose=%errorlevel%"
    
    :: you can have a maximum of 9 sites!
    set "select1=youtube.com facebook.com twitter.com instagram.com reddit.com"
    
    :: You can have a maximum of 9 IP's!
    set "select2=8.8.8.8 127.0.0.1 10.1.4.5"
    
    for %%i in (!select%chose%!) do (
        set /a cnt+=1
        echo !cnt!^) %%i
        set "_opt!cnt!=%%i"
    )
    for /l %%c in (1,1,%cnt%) do set ch=!ch!%%c
    choice /C %ch% /M "Enter Selection:"
    ping !_opt%errorlevel%!
    pause