You are not logged in.

#1 29 Feb 2016 18:22

sambul35
Member
Registered: 29 Feb 2016
Posts: 18

Wait in processing restricted user input

Hello guys,

I'm trying to code processing of a user input in a Win 10 64-bit batch, and the code becomes unstable when run as a part of a larger project, despite both the snippet and project code work OK standalone.

@echo off
rem Enter any number(s) within 5 sec each from a known array without repeating 
setlocal EnableDelayedExpansion
set "num=1 8 3 7 5 6 4 2 9" & set "lst=!num: =!" & set "count=1"
for %%i in (%num%) do set /a count+=1
echo/ & set /p "=Enter any number(s) from the pull: %num% (wait to exit) > "<nul

:input
choice /c %lst%Z /N /t 5 /d Z > nul
if errorlevel %count% (
   if not defined digit (
      echo/ & echo/ & choice /C YN /M "Exit the program?" /T 5 /D Y
      if !errorlevel! equ 1 (goto :end) else (echo/ & goto :input)
      )
   goto :cont
)
rem // repeat numbers input on the same line, filter out duplicates
set /a "pos=%errorlevel%-1"
set "digit=!lst:~%pos%,1!"
for %%k in (!cams!) do IF %%k equ !digit! GOTO :input
set "cams=!cams! !digit!"
set /P "=!digit! "<nul
set num=!num:~2!
if not !num!=="" goto :input

:cont
echo/ & echo/ & echo Entered numbers !cams!

:end
echo/ & echo Bye...

Entering 0 2 2 1 0 7 1 3 3 m n 3 3 3 7 3 k 3 1 1 1 1 will produce:

Entered numbers 2 1 7 3

The above code goes to :end , but doesn't want to go to the next label :cont or any other label no matter how many digits are entered, when used inside a larger project that works well otherwise.

What can be wrong here? At some point !nam! no longer exists, but even before that the code doesn't go to :cont in a larger project :?:  If I substitute :cont with :eof in IF statement it works perfect, but useless. Can you suggest any changes and/or explanation? I tried if !digit! equ 0 instead of if not defined, but for digit>0 it still doesn't go to :cont??? Instead in a larger code this function starts accepting any numbers seemingly ignoring the code below IF statement.

Also, is there a way to add a "caret return" (Enter) in choice /c parameters list?

Last edited by sambul35 (01 Mar 2016 03:44)

Offline

#2 29 Feb 2016 23:44

sambul35
Member
Registered: 29 Feb 2016
Posts: 18

Re: Wait in processing restricted user input

Just wanted to update the problem was elsewhere. I called a ":function" from another place in the code and sent the resulting !cams! array as a parameter to it. Apparently, Cmd truncates an "array" parameter, when sending it to a ":function", or when a variable is then assigned to such "parameter" in a ":function".

set "cams=1 2 3"
call :function %cams%

:function
set camz=%1
echo %camz%
goto :eof

echo is off

EDIT: I found the right syntax to send an array parameter to a function:

setlocal EnableDelayedExpansion
set "cams=1 2 3"
call :function "%cams%"

:function
set camz=%~1
echo !camz!
goto :eof

1 2 3

Last edited by sambul35 (01 Mar 2016 14:47)

Offline

#3 02 Mar 2016 00:51

sambul35
Member
Registered: 29 Feb 2016
Posts: 18

Re: Wait in processing restricted user input

Some user input validation routings are frequently repeated. I wanted to separate such routing as a function:

call :fun1 5 y ":end" ":input"

:fun1

if errorlevel %1 (
   choice /c yn /m "Exit the program?" /t 10 /d %2
   if !errorlevel! equ 1 (goto %~3) else (echo/ & goto %~4)
)
goto :eof

:end
exit /b

The problem however is, how to exit a batch from a ":function" without exiting Cmd (closing its window)? Upon execution :end code it just goes back to the place where it was called from. If I put exit instead of exit /b inside the function or under :exit label, Cmd window get closed.  hmm

Last edited by sambul35 (02 Mar 2016 00:53)

Offline

#4 02 Mar 2016 05:02

sambul35
Member
Registered: 29 Feb 2016
Posts: 18

Re: Wait in processing restricted user input

I found a couple of short code solutions to terminate a batch from function. One is offered by Max Suslov:

@echo off

:: comment next line if you want to export any local variables in caller environment
setlocal

set FLAG=1
rem Do something
call :interactive_check

set FLAG2=2
rem Do something
call :interactive_check

goto :eof

:interactive_check
if errorlevel 1 (
    echo.
    echo /!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\
    echo Error in compilation process... exiting
    echo /!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\
    (goto) 2>nul & endlocal & exit /b %ERRORLEVEL%
) else (
    echo Continuing to next step
)
goto :eof

The other by Charles Godwin:

@echo off
call :error message&if errorlevel 1 exit /b %errorlevel%<
@echo continuing
exit /b 0
:error
@echo in %0
@echo message: %1
set yes=
set /p yes=[no]^|yes to continue
if /i "%yes%" == "yes" exit /b 0
exit /b 1

Can someone explain, if and why these solutions allow to terminate the batch cleanly as suggested by their authors, as opposed to terminating by introducing a syntax error?

Last edited by sambul35 (02 Mar 2016 05:05)

Offline

Board footer

Powered by