Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combination of `sleep` and `pause` command

Ok, after seeing crazy stuff being completed in so little code, I have high hopes this is possible.

Pretty much, I want to use the pause command normally, however, if the user doesn't input anything for a specified duration of time, it automatically continues.

In pseudo code:

(sleep %sleep-time%&Echo Pass)1>0 & pause

I thought at first I could do this using start /b to create a process that echoed input while being paused i the current thread, but that could cause problems if the user does input something.

Bonus

What would be really cool is if the errorlevel would be changed based on whether the user inputted something, or if the pause command timed out.

like image 402
Monacraft Avatar asked Dec 03 '25 12:12

Monacraft


2 Answers

I suggest using timeout:

timeout /T 60 >NUL

This will sleep your script for 1 minute, or unless the user hits a key.

like image 145
GOTO 0 Avatar answered Dec 05 '25 13:12

GOTO 0


@echo off
setlocal

rem TimedPause.bat - Antonio Perez Ayala

if "%1" equ ":PausePart" goto PausePart
if "%1" neq "" goto begin
echo TimedPause.bat seconds
echo/
echo Wait for given seconds or until user press a key
echo At end, the presence of keyPressed.txt file indicate the cause of exit
goto :EOF

:begin
set seconds=%1
start "" /B "%~F0" :PausePart
for /F "skip=2 tokens=2 delims=," %%a in ('tasklist /FI "IMAGENAME eq cmd.exe" /FO CSV') do (
   set PausePart=%%a
   goto TimePart
)

:TimePart
   ping -n 2 localhost > NUL
   if exist keyPressed.txt goto :EOF
   set /A seconds-=1
if %seconds% gtr 0 goto TimePart
taskkill /PID %PausePart% /F > NUL
goto :EOF

:PausePart
del keyPressed.txt 2> NUL
pause
echo %time% > keyPressed.txt
exit
like image 38
Aacini Avatar answered Dec 05 '25 12:12

Aacini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!