0

I'm quite stuck on this one; I need a batch (most other programtypes are restricted on the system this has to be run on) to run about 10.000 times, then stop.

I got this far with this method: (generalized and simplified here, yet would do the trick to explain stuff with, because the file just clicks a bunch of stuff, just inhumanly fast)

@echo off
set loop=0
:start
echo Hello world.
set /a loop=%loop%+1
if "%loop%"=="10000" goto exit
goto start
:exit
exit

The above code is working, the problem I'm having is the risk. There's a small probability, the program I need this for starts sending keystrokes, which isn't that big of a problem, if the loop wouldn't still be running. The program could crash in a way causing windows to crash, which isn't quite what i need.

So the question I'm asking is: Is it possible to use something along the lines like if keystrokeinput==anything goto exit while the loop is running to stop it, or is there another method i could try?

If nothing works maybe a thirdpartyprogram that might do the trick?

I hope my needs and problems are clear

Edit: The keystroke needs to be checked like systemwide. If the batch needs to be focused/opened and visible, it wouldn't work.
The keystroke needs to be detected from outside of the according cmd, meaning if there is any input to the system.

Thanks in advance

Tim

Tim
  • 3
  • 4
  • If you're using something else in your loop, then it makes things clearer if you actually show us it instead of just some random, unrelated echo command. The less generic your code, the more specific your potential solutions will be. – Compo May 12 '20 at 13:06
  • 1
    Does this answer your question? [\[Batch\]Looping until a key is pressed](https://stackoverflow.com/questions/32163687/batchlooping-until-a-key-is-pressed) –  May 12 '20 at 13:12

2 Answers2

1

When a batch file is interrupted via Ctrl-C, AKA STATUS_CONTROL_C_EXIT, it returns ErrorLevel -1073741510 or ExitCode C000013A. We can take advantage of this.

@echo off
echo Press Y to break
call:function

NEVER REACHED
exit /b

:function
REM WORKS inside cached command block!
(
    echo Loop still in progress...
    choice /C ny /T 1 /D n /N>x
    <x >nul 2>&1 call :checkify
)
goto:function


:checkify
cmd /c exit -1073741510
ScriptKidd
  • 803
  • 1
  • 5
  • 19
1

Yes, keypress can be detected and used to exit a loop by running a multithreaded batch.

@Echo off

::: - Test if Key press thread is to be executed
    If /I "%~1" == "Wait" Goto :Wait

::: - Ensure previous Signal files removed in case program exited incorrectly
        Del /Q /F "%TEMP%\CMDrun.log"
        Del /Q /F "%TEMP%\CMDend.log"
    CLS

:Loop
::: - Launch second thread if not already running
    (IF Not Exist "%TEMP%\CMDrun.log" Start /B "" "%~F0" Wait) 2>Nul
    Set /A Count+=1
    Title %Count%
::: - Exit loop on second thread returning keypress
    (IF Exist "%TEMP%\CMDend.log" Goto :End_Loop) 2>Nul
If %Count% LSS 10000 Goto :Loop
:End_Loop
::: - Clean up Signal Files
    Echo. 
    Title Example Complete at count %count%
    Del /Q /F "%TEMP%\CMDrun.log"
    Del /Q /F "%TEMP%\CMDend.log"
    Pause
    Exit

:Wait
::: - Signal main thread run status
    >"%TEMP%\CMDrun.log" Echo Running
::: - Detect keypress
    For /F "Delims=" %%A in ('xcopy /w "%~f0" "%~f0" 2^>nul') do (
        If Not Defined Key Set "Key=%%A"
    )
::: - return keypress in 'end' signal file
    If defined key (
        Setlocal EnableDelayedExpansion
        Set "Key=!Key:~-1!"
        >"%TEMP%\CMDend.log" Echo !Key! && EXIT
        Endlocal
    )
::: - continue waiting for kepress
Goto :Wait
T3RR0R
  • 2,747
  • 3
  • 10
  • 25
  • That's almost what I needed, pretty damn close to be honest. But the problem with this setup is the fact, that I need to be on the according cmd to stop it, but that's not possible. The program the batch needs to be run on has to be interacted with within the loop. Would it be possible to make the keystroke get checked even while outside of the cmd? – Tim May 13 '20 at 12:59
  • 2
    I'm not aware of a method that can be used in a running batch to detect a keypress when the batch is not active. I've experimented with batch scripts using Aacini's GetKey.exe without success. This doesn't mean this can't be achieved, you'd just need to find a keylogger that can write to a file your batch can monitor – T3RR0R May 13 '20 at 14:08
  • Okay a Keylogger may be the way to go. So if I find one I can execute, would it be as simple as telling the script to check for changes in the certain document? Just as a little question; A Keylogger isn't possible with a batch, right? – Tim May 13 '20 at 22:16
  • Not directly possible from batch, no. Your at the point now of asking a new question, relating to a keylogger and how to control and interact with it in batch. I suggest asking that question over on superuser or dostips.com – T3RR0R May 14 '20 at 07:49