Page 1 of 1

How to interrupt script by closing the CMD window with any key?

Posted: 06 Sep 2023 10:22
by DOSadnie
I wrote this BAT so that I would not have to perform by hand all such tasks needed to play the game

Code: Select all

@echo off

:: welcoming text:

echo. Commencing Apocalypse
echo. PRESS ANY KEY TO CANCEL

:: fail-safe:

timeout /t 5 | findstr /R ".0$" >nul

:: no-goes:

taskkill /im HuionTablet.exe >nul 2>nul
timeout /t 1 >nul
taskkill /im HuionTablet.exe /t /f  >nul 2>nul
timeout /t 1 >nul

:: the game:

pushd P:\Game Play\Fallout 4 GOTYE\
start "" /b /max Fallout4.exe
popd

:: necessities:

cd "C:\Program Files (x86)\MSI Afterburner\"
start "" /b /min MSIAfterburner.exe
timeout /t 1 >nul

cd "C:\Program Files (x86)\RivaTuner Statistics Server\"
start "" /b /min RTSS.exe
timeout /t 1 >nul

start /b "" "C:\q\! SCRIPTS\Game Controls Binding.. Fallout #4.ahk"
timeout /t 1 >nul

:: closure:

exit
How can I make it close itself immediately without going further down its own code by pressing any key?

Re: How to interrupt script by closing the CMD window with any key?

Posted: 07 Sep 2023 11:11
by aGerman
It's okay to use findstr. However, at least you need to check the errorlevel value it returned.

Code: Select all

@echo off &setlocal

set "delay=5"
:: prompt without new line
<nul set /p "=PRESS ANY KEY TO CANCEL within the next %delay% seconds ... "
:: check if the output ends with 0 that isn't preceded by another digit (which can happen if 'delay' is defined >10)
timeout /t %delay% | >nul findstr /e "[^0-9]0"
:: get outta here if no trailing 0 found
if errorlevel 1 exit /b
:: else, write the new line that has been omitted in the prompt
echo(

echo timed out, continue your script here ...
pause
Steffen

Re: How to interrupt script by closing the CMD window with any key?

Posted: 10 Sep 2023 03:00
by DOSadnie
This works; almost - just like my slightly reworked version [with descriptions for dummies like me]:

Code: Select all

@echo off

echo.  Commencing this script
echo. PRESS ANY KEY TO CANCEL

:: Set the latter >>timeout<< command in seconds:
set LENGTH_OF_THE_COUNTDOWN=3

:: Check if the output ends with zero that is not preceded by another digit [which is possible if the countdown will be defined as greater than 10 - i.e. the >>findstr /e "[^0-9]0<< makes possible to use also a >>timeout<< value that consists of more than one digit when the last digit happens to be >>0<< [e.g. >>20<<]:
timeout /t %LENGTH_OF_THE_COUNTDOWN% | >nul findstr /e "[^0-9]0"

:: Terminate this script if no trailing 0 is found - i.e. close this BAT file if >>timeout<< command was interrupted by pressing of a key or when the countdown ends:
if errorlevel 1 exit /b

:: Info to be displayed when user does not interrupt the countdown - i.e. when user does not abort execution by pressing of any key
echo. The countdown has ended

:: Place for other commands that are to be executed after the countdown:
pause
Because for whatever reason, from what I have accidentally noticed, the CMD window does not close if I press the

Code: Select all

w
key. And no, I have no other scripts running and the >>w<< does not work even if press it virtually with e.g. On-Screen Keyboard [while other virtually executed keys do work i.e. they close this BAT]

Re: How to interrupt script by closing the CMD window with any key?

Posted: 10 Sep 2023 03:58
by aGerman
The only remaining bug related to timeout.exe that I know of is one of a couple "DefTerm" bugs caused by the Windows Terminal set the default terminal host.
https://github.com/microsoft/terminal/issues/14499
However, that's probably not the case here because the timeout command would have always returned immediately then.

Steffen

EDIT: Now that I'm on my computer I confirm that timeout completely ignores the w-key for watever reason. Even without the redirection to findstr and even when capslock is turned on. Weird.

Re: How to interrupt script by closing the CMD window with any key?

Posted: 18 Sep 2023 06:44
by DOSadnie
Apparently it is the

Code: Select all

timeout
command that is creating this issue. Because such test script works

Code: Select all

@echo off

setlocal enabledelayedexpansion

echo. Press any key to close this BAT

pause >nul

exit /b
i.e. pressing just once of even the >>w<< key closes it - while this version

Code: Select all

@echo off

setlocal enabledelayedexpansion

echo. Test text

timeout /t 5

pause >nul

exit /b
needs not only two pressings of any key but in case of >>w<< is succumbs to its second pressing only when timeout has ended. And one pressing of any key makes the prompt sign jump from the end of the
Waiting for 4 seconds, press a key to continue ...
line to a new empty one


This is the second time I have stumbled upon timeout command being the culprit messing up things: viewtopic.php?f=3&t=10865&p=68923&hilit=timeout#p68923


Is there a way to workaround this? I need to be able using physically keyboard to interrupt that countdown before further steps are executed - but using countdown in form of timeout makes >>w<< incapable of interacting with CMD window

Re: How to interrupt script by closing the CMD window with any key?

Posted: 18 Sep 2023 11:38
by ShadowThief
Well you need to press any key twice because the first key ends the timeout command and the second key ends the pause command.

Very odd that timeout doesn't care about the w key though.

Re: How to interrupt script by closing the CMD window with any key?

Posted: 13 Oct 2023 08:49
by DOSadnie
DOSadnie wrote:
18 Sep 2023 06:44
[...]
I need to be able using physically keyboard to interrupt that countdown before further steps are executed - but using countdown in form of timeout makes >>w<< incapable of interacting with CMD window
This is an alternative method

Code: Select all

timeout /t 5 | findstr /R ".0$" > nul
&& goto continue || exit

command > nul 2>&1
:continue
but it also does not make the CMD window succumb to pressing of >>w<<

Re: How to interrupt script by closing the CMD window with any key?

Posted: 18 Nov 2023 11:27
by DOSadnie
DOSadnie wrote:
13 Oct 2023 08:49
[...]
does not make the CMD window succumb to pressing of >>w<<
I have just made a new topic about this >>W<< issue:
viewtopic.php?f=3&t=10945