Question

I want to add a line (or lines) to my batch file

if (ctrl + g is pressed) goto end

How to do this?

Was it helpful?

Solution

@ECHO OFF
SETLOCAL
SET controlg=
ECHO Press control-g to exit
:again
SET "key="
For /F "delims=" %%z In ('Xcopy /W "%~f0" "%~f0" 2^>Nul') Do If Not Defined Key Set "Key=%%z"
SET "key=%key:~-1%"
IF NOT "%key%"=="%controlg%" GOTO again
ECHO now exiting...
ECHO exiting...
GOTO :EOF

It's not perfect by any means...There is actually a control-g character being set into the variable, even if it's not being shown. Naturally, that could be placed in the if not ... statement, it's just how I do things...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top