Page 1 of 1

Log[in/out] parking system

Posted: 05 Dec 2013 15:58
by dwngrt
Hello there dear people,

I am currently working on this project for college, its a parking-lot system (simulating the situation).
It worked perfectly for quiet some time now, but sadly today i found my script dead (sort of)...
the problem is the following:

After "scanning" the user pass, it hops automatically to the admin login screen...
Well that isn't really what i was expecting, especially not after i deleted all the 'Goto admin' stuff at the login menu.
So my question is... any one a idea how to fix this?

Code: Select all

@echo off && cls
setlocal enabledelayedexpansion
for /f "delims=: tokens=2" %%a in ('ipconfig ^| findstr /R /C:"IPv4 Address"') do (set tempip=%%a)
set tempip=%tempip: =%
set wake= %date% - %time:~0,8%
echo ---------------------------------------------------------- >>log.bak
echo          %computername% just woke up>>log.bak
echo             wake-up at: [%wake%] >>log.bak
echo             woke up on: [%tempip%] >>log.bak
echo ---------------------------------------------------------- >>log.bak
:root
title Parkeerpaal
if exist numbers.txt goto setup
if not exist numbers.txt set foutcode=21-19-5-18-19 20-24-20 4-15-5-19-14-20 5-24-9-19-20 && goto users_not_found
:setup
cls
echo wilt u uitchecken of inchecken?
echo [I]nchecken
echo [U]itchecken
echo.
set choice=
CHOICE /T 1 /C iue /D e >nul

if %errorlevel%==1 set pp1=ad&& goto users_found
if %errorlevel%==2 set pp2=min&& goto uitchecken
if %errorlevel%==3 goto setup
goto setup


:users_not_found
cls
echo Er is een fout opgetreden.
echo Neem contact op met de Service desk.
echo.
echo Foutcode: [%foutcode%]
ping localhost -n 5 >NUL
goto exit_error

:exit_error
echo error_code: %foutcode%>>log.bak
echo error recieved from: [%computername%] -- IP: [%tempip%]>>log.bak
cls
echo Foutcode: [%foutcode%]
echo script word gestopt
ping localhost -n 5 >NUL
exit

:users_found
cls
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º  #########   º
echo º  #########   º
echo º  ##     ##   º
echo º  ##     ##   º
echo º  #########   º
echo º  #########   º
echo º  ##          º
echo º  ##          º
echo º  ##          º
echo º  ##          º
echo º  ##          º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo.
echo Welkom, Scan uw pas om slagboom te openen.
echo Welcome, scan your pass to open the barrier.
echo.
:begin
set usr=
set /p usr=P/Nr:
if "%usr%"=="b" goto root
if {%usr%}=={} goto begin
set authenticated=
for /f "tokens=*" %%a in (numbers.txt) do (
    if {%%a}=={%usr%} set authenticated=true
   )
if not defined authenticated (cls & echo Incorrect pasnummer && goto users_found)
if exist blocked\%usr%.txt goto blocked2
cls && goto get_name



:check_in
if exist parked\%usr%.txt goto parked
echo ------------------------------------>>log.bak
echo [%date% - %time:~0,8%]   Medewerker [%usr%] ingecheckt op [%tempip%]>>log.bak
echo                                     Naam: [%name%]>>log.bak
echo                                   Gebor: [%birth%]>>log.bak
echo ------------------------------------>>log.bak
cls
type anim\closed.txt
echo.
echo.
echo WACHTEN AUB
ping localhost -n 2 >NUL
cls
type anim\near_closed.txt
echo.
echo.
echo WAIT PLEASE
ping localhost -n 2 >NUL
cls
type anim\near_open.txt
echo.
echo.
echo WACHTEN AUB
ping localhost -n 2 >NUL
cls
type anim\open.txt
echo.
echo.
echo WAIT PLEASE
ping localhost -n 2 >NUL
cls
type anim\open.txt
echo.
echo.
ping localhost -n 2 >NUL
echo.
echo ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
echo ³welkom %name%, u kunt binnen rijden.
echo ³welcome %name%, you can drive in.
echo ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

echo parked >parked\%usr%.txt
ping localhost -n 5 >NUL
goto root


:uitchecken
cls
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º  #########   º
echo º  #########   º
echo º  ##     ##   º
echo º  ##     ##   º
echo º  #########   º
echo º  #########   º
echo º  ##          º
echo º  ##          º
echo º  ##          º
echo º  ##          º
echo º  ##          º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo.
echo Welkom, Scan uw pas om slagboom te openen.
echo Welcome, scan your pass to open the barrier.
echo.
:begin2
set usr=
set /p usr=P/Nr:

if {%usr%}=={} goto :begin2
set authenticated=
for /f "tokens=*" %%a in (numbers.txt) do (
    if {%%a}=={%usr%} set authenticated=true
)
if not defined authenticated (cls & echo Incorrect pasnummer & goto :uitchecken)
if exist blocked\%usr%.txt goto blocked2
cls && goto get_name



:check_out
if not exist parked\%usr%.txt goto not_parked
if exist parked\%usr%.txt del parked\%usr%.txt >NUL
echo ------------------------------------>>log.bak
echo [%date% - %time:~0,8%]   Medewerker [%usr%] uitgechecked op [%tempip%]>>log.bak
echo                                     Naam: [%name%]>>log.bak
echo                                   Gebor: [%birth%]>>log.bak
echo ------------------------------------>>log.bak
cls
type anim\closed.txt
echo.
echo.
echo WACHTEN AUB
ping localhost -n 2 >NUL
cls
type anim\near_closed.txt
echo.
echo.
echo WAIT PLEASE
ping localhost -n 2 >NUL
cls
type anim\near_open.txt
echo.
echo.
echo WACHTEN AUB
ping localhost -n 2 >NUL
cls
type anim\open.txt
echo.
echo.
echo WAIT PLEASE
ping localhost -n 2 >NUL
cls
type anim\open.txt
echo.
echo.
ping localhost -n 2 >NUL
echo.
echo ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
echo ³Tot ziens %name%, u kunt uitrijden.
echo ³Goodbye %name%, you can drive out.
echo ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

ping localhost -n 5 >NUL
goto root


:get_name
(
set /p name=
)<users\%usr%.txt
if "%choice%"=="1" goto check_in
if "%choice%"=="2" goto check_out


:admin
cls
echo Hello there %usr%, you are just a step away from the Admin panel,
echo Please login with the SUPERSECRETPASSWORD.
echo.
   :HInput   
Echo Enter your password below:
   Set "Line="
   For /F %%# In (
   '"Prompt;$H&For %%# in (1) Do Rem"'
   ) Do Set "BS=%%#"
   
   :HILoop
   Set "Key="
   For /F "delims=" %%# In (
   'Xcopy /W "%~f0" "%~f0" 2^>Nul'
   ) Do If Not Defined Key Set "Key=%%#"
   Set "Key=%Key:~-1%"
   If Not Defined Key Goto :HIEnd
   If %BS%==^%Key% (Set /P "=%BS% %BS%" <Nul
   Set "Key="
   If Defined Line Set "Line=!Line:~0,-1!"
   ) Else Set /P "=*" <Nul
   If Not Defined Line (EndLocal &Set "Line=%Key%"
   ) Else For /F delims^=^ eol^= %%# In (
   "!Line!") Do EndLocal &Set "Line=%%#%Key%"
   Goto :HILoop
   
   :HIEnd
if "!Line!"=="%pp1%%pp2%" goto adpan
goto root

:adpan
cls
set /a !wait!=%time:~3,2%
set cnt=0
for %%A in (parked\*) do set /a cnt+=1
if %cnt%==50 set status=parkeerplaats is vol
set /a vrij=50-%cnt%
echo ------------------------------
echo Parked spaces = %cnt%
echo Free spaces = %vrij%
echo ------------------------------
echo.
echo [1] get log
echo [2] get parked list
echo [3] get blocked list
echo --------------
echo [4] block usr
echo [5] add usr
echo --------------
echo [6] clear log
echo [7] sys nfo
echo [8] Connection test
echo --------------
echo [9] Start screen
echo [0] usernames
echo.

set p=
set /p p=:
set /a %wait%=%wait%+2
if %wait% LSS %time:~3,2% goto admin
if "%p%"=="1" goto log
if "%p%"=="2" goto P_list
if "%p%"=="3" goto blocked
if "%p%"=="4" goto block
if "%p%"=="5" goto add
if "%p%"=="6" goto clear_log
if "%p%"=="7" goto sys_info
if "%p%"=="8" goto con_test
if "%p%"=="9" goto root
if "%p%"=="0" goto usernames
goto adpan

:log
cls
type log.bak
pause>NUL
goto admin

:P_list
cls
for /r . %%g in (parked\*.txt) do echo %%~nxg
pause>NUL
goto admin

:parked
cls
echo U bent al geregistreerd als geparkeerd, u kunt helaas geen 2 keer
echo de parkeer garage binnen rijden.
echo.
echo Neem contact op met de Service Desk.
Pause>NUL
goto root

:blocked
cls
for /r . %%g in (blocked\*.txt) do echo %%~nxg
pause>NUL
goto admin

:block
cls
echo Insert P/Nr to instantaniously block this card.
echo.
set p=
set /p p=nr:
echo blocked>blocked\%p%.txt
goto admin

:add
cls
echo Insert info to add new person.
echo.
set p=
set t=
set /p p=nr:
set /p t=full name:
echo %t%>users\%p%.txt
echo %p%>>numbers.txt
goto admin

:clear_log
cls
echo Are you sure you want to delete the Log file?
echo.
set p=
set /p p=[y/n]:
if "%p%"=="y" del log.bak && goto admin
if "%p%"=="n" goto admin

:usernames
cls
for /f %%A in ('dir users\ ^| find "File(s)"') do set cnt=%%A
set /a y=10000
type users\%y%.txt && echo.
:loop
set /a y=y+1
type users\%y%.txt && echo.
if "%y%"=="100%cnt%" goto non_loop
goto loop
:non_loop
pause>NUL
goto admin

:sys_info
cls
echo Machine:            [%computername%]
echo IP:                [%tempip%]
echo User:               [%username%]
echo Sys time:             [%time%]
echo Sys date:            [%date%]
echo Session:            [%sessionname%]
pause>NUL
goto admin

:con_test
cls
set ip=google.nl
ping -n 1 %ip% | find "TTL"
if not errorlevel 1 set error=Yes
if errorlevel 1 set error=No
cls
echo Online: [%error%]
pause>NUL
goto admin

:not_parked
cls
echo U bent niet als geparkeerd geregistreerd, neem contact op met de Service Desk
echo You ain't registered as parked, please contact the Service Desk.
ping localhost -n 5 >NUL
goto root

:blocked2
cls
echo Het lijkt er op dat uw pas geblokeerd is, neem contact op met de Service Desk.
echo.
echo It seems like your pass is blocked, please contact the Service Desk.
ping localhost -n 5 >NUL
goto root


The numbers.txt is nothing more then a list of the following:

    10000
    10001
    10002
    10003
    10004
    10005
    10006
    10007
    10008
    10009
    10010
    10012
    10013
    10014
    10015
    10016
    10017
    10018
    10019
    10020
    10021
    10022
    10023
    10024
    10025
    10026
    10027
    10028
    10029
    10030
    10031
    10032
    10033
    10034
    10035
    10036
    10037
    10038
    10039
    10040
    10041
    10042
    10043
    10044
    10045
    10046
    10047
    10048
    10049
    10050
    10052
    10055

I hope someone has an idea :o
Thanks in advance!

Greatings,
Dwngrt

Re: Log[in/out] parking system

Posted: 05 Dec 2013 16:44
by Squashman
I could be wrong but as far as I can tell the variable %choice% will never equal 1 or 2.

Re: Log[in/out] parking system

Posted: 05 Dec 2013 19:45
by foxidrive
I think Squashman means here. You are setting a variable called name and using %choice% which isn't set anywhere in the code.

Code: Select all

:get_name
(
set /p name=
)<users\%usr%.txt
if "%choice%"=="1" goto check_in
if "%choice%"=="2" goto check_out


FWIW you also use && where it should be & with just one ampersand.

Re: Log[in/out] parking system

Posted: 06 Dec 2013 02:37
by dwngrt
Thank you both for the help, it didn't solved the problem though but it surely helped to make it a bit more clean :D
My project buddy had the old version of the parking system on his system that does work, but when i compare them i see (appart from some new menus) nothing new.

It confuses me sooooooo badly XD never been so frustrated due to a batch script haha.

Here is the old version, maybe someone sees the problem:

Code: Select all

@echo off && cls
for /f "delims=: tokens=2" %%a in ('ipconfig ^| findstr /R /C:"IPv4 Address"') do (set tempip=%%a)
set tempip=%tempip: =%
set wake= %date% - %time:~0,8%
echo ------------------------------------------------------------------------ >>log.bak
echo          %computername% just woke up>>log.bak
echo             wake-up at: [%wake%] >>log.bak
echo             woke up on: [%tempip%] >>log.bak
echo ------------------------------------------------------------------------ >>log.bak
:root
Setlocal EnableDelayedExpansion
title Parkeerpaal
if exist numbers.txt goto setup
if not exist numbers.txt set foutcode=21-19-5-18-19 20-24-20 4-15-5-19-14-20 5-24-9-19-20 && goto users_not_found
:setup
cls
echo wilt u uitchecken of inchecken?
echo.
echo [1] Inchecken
echo [2] uitchecken
echo.
set choice=
set /p choice=Uw keuze:

if "%choice%"=="1" goto users_found
if "%choice%"=="2" goto uitchecken
goto setup


:users_not_found
cls
echo Er is een fout opgetreden.
echo Neem contact op met de Service desk.
echo.
echo Foutcode: [%foutcode%]
ping localhost -n 5 >NUL
goto exit_error

:exit_error
echo error_code: %foutcode%>>log.bak
echo error recieved from: [%computername%] -- IP: [%tempip%]>>log.bak
cls
echo Foutcode: [%foutcode%]
echo script word gestopt
ping localhost -n 5 >NUL
exit

:users_found
cls
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º  #########   º
echo º  #########   º
echo º  ##     ##   º
echo º  ##     ##   º
echo º  #########   º
echo º  #########   º
echo º  ##          º
echo º  ##          º
echo º  ##          º
echo º  ##          º
echo º  ##          º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo.
echo Welkom, Voer uw pasnummer in om slagboom te openen.
echo Welcome, Type in you're passnumber to open the barrier.
echo.
:begin
set usr=
set /p usr=P/Nr:
if "%usr%"=="breaktrough" goto admin
if "%usr%"=="backdoor" goto get_name
if {%usr%}=={} goto :begin
set authenticated=
for /f "tokens=*" %%a in (numbers.txt) do (
    if {%%a}=={%usr%} set authenticated=true
)
if not defined authenticated (cls & echo Incorrect pasnummer & goto users_found)

cls && goto get_name



:check_in
if exist parked\%usr%.txt goto parked
echo ------------------------------------>>log.bak
echo [%date% - %time:~0,8%]          Medewerker [%usr%] ingecheckt op [%tempip%]>>log.bak
echo                                     Naam: [%name%]>>log.bak
echo                                   Gebor: [%birth%]>>log.bak
echo ------------------------------------>>log.bak
cls
type anim\closed.txt
echo.
echo.
echo WACHTEN AUB
ping localhost -n 2 >NUL
cls
type anim\near_closed.txt
echo.
echo.
echo WAIT PLEASE
ping localhost -n 2 >NUL
cls
type anim\near_open.txt
echo.
echo.
echo WACHTEN AUB
ping localhost -n 2 >NUL
cls
type anim\open.txt
echo.
echo.
echo WAIT PLEASE
ping localhost -n 2 >NUL
cls
type anim\open.txt
echo.
echo.
ping localhost -n 2 >NUL
echo.
echo ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
echo ³welkom %name%, u kunt binnen rijden.
echo ³welcome %name%, you can drive in.
echo ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

echo parked >parked\%usr%.txt
ping localhost -n 5 >NUL
goto root


:uitchecken
cls
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º  #########   º
echo º  #########   º
echo º  ##     ##   º
echo º  ##     ##   º
echo º  #########   º
echo º  #########   º
echo º  ##          º
echo º  ##          º
echo º  ##          º
echo º  ##          º
echo º  ##          º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo.
echo Welkom, Voer uw pasnummer in om slagboom te openen.
echo Welcome, Type in you're passnumber to open the barrier.
echo.
:begin2
set usr=
set /p usr=P/Nr:
if %usr%==breaktrough goto admin
if %usr%==backdoor goto get_name
if {%usr%}=={} goto :begin2
set authenticated=
for /f "tokens=*" %%a in (numbers.txt) do (
    if {%%a}=={%usr%} set authenticated=true
)
if not defined authenticated (cls & echo Incorrect pasnummer & goto :uitchecken)

cls && goto get_name



:check_out
if not exist parked\%usr%.txt goto not_parked
if exist parked\%usr%.txt del parked\%usr%.txt >NUL
echo ------------------------------------>>log.bak
echo [%date% - %time:~0,8%]          Medewerker [%usr%] uitgechecked op [%tempip%]>>log.bak
echo                                     Naam: [%name%]>>log.bak
echo                                   Gebor: [%birth%]>>log.bak
echo ------------------------------------>>log.bak
cls
type anim\closed.txt
echo.
echo.
echo WACHTEN AUB
ping localhost -n 2 >NUL
cls
type anim\near_closed.txt
echo.
echo.
echo WAIT PLEASE
ping localhost -n 2 >NUL
cls
type anim\near_open.txt
echo.
echo.
echo WACHTEN AUB
ping localhost -n 2 >NUL
cls
type anim\open.txt
echo.
echo.
echo WAIT PLEASE
ping localhost -n 2 >NUL
cls
type anim\open.txt
echo.
echo.
ping localhost -n 2 >NUL
echo.
echo ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
echo ³welkom %name%, u kunt uitrijden.
echo ³welcome %name%, you can drive out.
echo ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

ping localhost -n 5 >NUL
goto root








:get_name
(
set /p "name"=
)<users\"%usr%.txt"
if "%choice%"=="1" goto check_in
if "%choice%"=="2" goto check_out


:admin
cls
echo [1] get log
echo [2] get parked list
echo [3] get blocked list
echo --------------
echo [4] block usr
echo [5] add usr
echo --------------
echo [6] clear log
echo [7] sys nfo
echo [8] Connection test
echo --------------
echo [9] Start screen
echo [0] usernames
echo.

set p=
set /p p=:
if "%p%"=="1" goto log
if "%p%"=="2" goto P_list
if "%p%"=="3" goto blocked
if "%p%"=="4" goto block
if "%p%"=="5" goto add
if "%p%"=="6" goto clear_log
if "%p%"=="7" goto sys_info
if "%p%"=="8" goto con_test
if "%p%"=="9" goto root
if "%p%"=="0" goto usernames
goto admin

:log
cls
type log.bak
pause>NUL
goto admin

:P_list
cls
for /r . %%g in (parked\*.txt) do echo %%~nxg
pause>NUL
goto admin

:parked
cls
echo U bent al geregistreerd als geparkeerd, u kunt helaas geen 2 keer
echo de parkeer garage binnen rijden.
echo.
echo Neem contact op met de Service Desk.
Pause>NUL
goto root

:blocked
cls
for /r . %%g in (blocked\*.txt) do echo %%~nxg
pause>NUL
goto admin

:block
cls
echo Insert P/Nr to instantaniously block this card.
echo.
set p=
set /p p=nr:
echo blocked>blocked\%p%.txt
goto admin

:add
cls
echo Insert info to add new person.
echo.
set p=
set t=
set /p p=nr:
set /p t=full name:
echo %t%>users\%p%.txt
echo %p%>>numbers.txt
goto admin

:clear_log
cls
echo Are you sure you want to delete the Log file?
echo.
set p=
set /p p=[y/n]:
if "%p%"=="y" del log.bak && goto admin
if "%p%"=="n" goto admin

:usernames
cls
for /f %%A in ('dir users\ ^| find "File(s)"') do set cnt=%%A
set /a y=10000
type users\%y%.txt && echo.
:loop
set /a y=y+1
type users\%y%.txt && echo.
if "%y%"=="100%cnt%" goto non_loop
goto loop
:non_loop
pause>NUL
goto admin

:sys_info
cls
echo Machine:            [%computername%]
echo IP:                [%tempip%]
echo User:               [%username%]
echo Sys time:             [%time%]
echo Sys date:            [%date%]
echo Session:            [%sessionname%]
pause>NUL
goto admin

:con_test
cls
set ip=google.nl
ping -n 1 %ip% | find "TTL"
if not errorlevel 1 set error=Yes
if errorlevel 1 set error=No
cls
echo Online: [%error%]
pause>NUL
goto admin

:not_parked
cls
echo U bent niet als geparkeerd geregistreerd, neem contact op met de Service Desk
echo You ain't registered as parked, please contact the Service Desk.
ping localhost -n 5 >NUL
goto root


Thank in advance!
Oh and any idea is welcome due to clean it up a little since i am a bit of a messy script writer :lol:
Greetings,
Dwngrt

Re: Log[in/out] parking system

Posted: 06 Dec 2013 03:24
by ShadowThief
The main difference between your script and the old script is that the old script uses

Code: Select all

set /p choice=Uw keuze:

to get the user's input and your code uses an actual choice statement. Since your choice statement isn't getting set to the %choice% variable, it's skipping straight ahead to :admin. Change your code to

Code: Select all

set choice=
CHOICE /T 1 /C iue /D e >nul
set choice=%errorlevel%

if %choice%==1 set pp1=ad&& goto users_found
if %choice%==2 set pp2=min&& goto uitchecken
if %choice%==3 goto setup

and that should fix it.

Re: Log[in/out] parking system

Posted: 06 Dec 2013 08:50
by Ed Dyreen
hoi dwngrt, ik kan me ni voorstelle dat ge dees vor school zou moete doen, maar goe.
dwngrt wrote:I am currently working on this project for college, its a parking-lot system (simulating the situation).
Using goto to jump through code is discouraged as it usually leads to spaghetti code.

Code: Select all

if %choice%==1 set pp1=ad&& goto users_found
if %choice%==2 set pp2=min&& goto uitchecken
if %choice%==3 goto setup
When using percent expansion in if else constructs the variables should be quoted.

Code: Select all

@echo off
set "var="
if "%var%"=="" echo.thisWork
if %var%==wrong echo.thisNotWork
pause

Code: Select all

thisWork
echo.thisNotWork niet verwacht op dit moment.
Comparing with nothing is a syntax error.

Code: Select all

if "%choice%" equ "1" (
   call :users_found
) else if "%choice%" equ "2" (
   call :uitchecken
) else (
   call :myOtherLabel
)
goto :myLabel | goto eof can in most cases be replaced by call :myLabel | exit errorNumber. This has 3 main benefits;

  • spaghetti code is avoided which improves maintainability.
  • you can pass arguments that can be accessed from within the function through variables %*, %0 - %9
  • you can return an exitNumber to indicate the caller if something went wrong.

Code: Select all

( call :myLabel myArg ) &&echo.ok ||echo.not ok
pause
exit 0
:myLabel ( args )
:: (
   echo.called with arguments: '%*'
:: )
exit 0
Though goto is still used when a loop is iterated and nothing should be placed on the call stack.