1

I have a script that searches for a running process (notepad) and it will stay runnning until notpad is no longer running:

:search
TASKLIST|FIND "notepad.exe" >nul 2>&1
IF %ERRORLEVEL% equ 0 (GOTO found)
timeout 3 > nul
GOTO search

:found
exit

What i'd like to insert here is a prompt for keypress to exit this loop and jump to :foundIf no keypress is entered the file keep looking for notepad until it closes.

Kalamalka Kid
  • 211
  • 4
  • 11
  • 2
    Type `waitfor /?` Instead of `timeout` use `waifor /t 3 cat`. When you kill it send in another batchfile `waitfor /si cat`. –  Jun 03 '20 at 06:48
  • @Mark what is cat? – Kalamalka Kid Jun 03 '20 at 07:20
  • A signal. I had to think of a word. I choose cat. Make it dog. In help they use `CopyDone` as an example.. If you create a shortcut to the second batch you can assign a hotkey in Properties. –  Jun 03 '20 at 07:44
  • thanks but I am not looking to make a second batch file. I simply want to be able to interrupt the script above with a keystroke if that is possible. – Kalamalka Kid Jun 03 '20 at 08:09
  • Ctrl+C interrupts batchfiles. –  Jun 03 '20 at 08:52
  • 2
    See duplicate question [here](https://stackoverflow.com/a/61754467/12343998) – T3RR0R Jun 03 '20 at 09:46
  • 1
    Nice idea to use `waitfor`, @Mark! you don't even have to use a second batch file, you can let a batch file call itself in a new `cmd` instance (similar to the answer user @T3RR0R linked to), so one instance waits and the other one sends the signal… – aschipfl Jun 03 '20 at 11:32

1 Answers1

2

I corrected your logic a bit (to match stay running until notepad is no longer running)

:search
TASKLIST|FIND "notepad.exe" >nul 2>&1
IF errorlevel 1 GOTO :cont
choice /c cn  /t 3 /d n /n /m "[C]ancel"
if errorlevel 2 goto :search
echo you cancelled.
goto :eof

:cont
echo Notepad has closed.
exit /b

For an explanation of the choice command see choice /?
For an explanation of the changed if errorlevel syntax, see if /?

or - if you don't like the continuous [C]ancel - suppress the output of choice and print the hint before the loop:

echo [C]ancel
:search
TASKLIST|FIND "notepad.exe" >nul 2>&1
IF errorlevel 1 GOTO :cont
choice /c cn  /t 3 /d n >nul
if errorlevel 2 goto :search
echo you cancelled.
goto :eof

:cont
echo Notepad has closed.
exit /b
Stephan
  • 50,835
  • 10
  • 55
  • 88
  • works perfectly. Wondering how I might replace C to cancel with ENTER? – Kalamalka Kid Jun 03 '20 at 15:34
  • 1
    nope. `choice` supports [a-z], [A-Z], [0-9] and ASCII between 128 and 254. If you really need "ENTER" (or "AnyKey") there is an ugly `xcopy` hack (not recommended unless absolutely necessary. You can extract it from [here](https://www.dostips.com/forum/viewtopic.php?t=4741)) – Stephan Jun 03 '20 at 15:46
  • ok thanks. Is there another way instead of using `choice` to jump out of a loop using `ANYKEY` or `ENTER`? Is this worthy of a new question? – Kalamalka Kid Jun 03 '20 at 15:51
  • If you find a promising way and just can't quite make it work, yes, ask another question. If not, it's just a code request and off-topic for this site. Did you take a look at the possible duplicate, @T3RR0R mentioned? – Stephan Jun 03 '20 at 16:05
  • This solution is useful, but you could do away with a chunk of it by using the errorlevel with default timeout switches for the choice command with labels :0 and :1 - so long as you don't mind passing up on the cancelled / closed feedback – T3RR0R Aug 27 '20 at 17:31