Batch files can not supress "terminate job" - windows

Im trying to open a 2nd batch file and detect if it normally exited or closed by a user (ctrl+c or x or window termiate etc..)
so Im using this following example by Batch run script when closed
#Echo off
set errorlevel=1
start /w %comspec% /c "mode 70,10&title Folder Confirmation Box&color 1e&echo.&echo. Else the close window&pause>NUL&exit 12345"
echo %errorlevel%
pause
Im trying to keep 1st batch waiting (/W) since I will check for errorlevel later on
But after closing the 2nd batch file I get an error like ^cterminate batch job (Y/N)?
I tried the suggestion over https://superuser.com/questions/35698/how-to-supress-terminate-batch-job-y-n-confirmation
with the script
rem Bypass "Terminate Batch Job" prompt.
if "%~2"=="-FIXED_CTRL_C" (
REM Remove the -FIXED_CTRL_C parameter
SHIFT
) ELSE (
REM Run the batch with <NUL and -FIXED_CTRL_C
CALL <NUL %1 -FIXED_CTRL_C %*
GOTO :EOF
)
That works quite fine
So is there a way of starting from same batch file and avoiding the terminating?
Or do I have to create a new batch from same batch and call it?
(I don't want them to see the file aswell)

Do not assign values to a volatile environment variable like errorlevel using set command. Doing that causes it becomes unvolatile in current context.
Always use title in START "title" [/D path] [options] "command" [parameters].
start "" /W cmd /c "anycommand&exit /B 12345" always returns 12345 exit code. It's because all the cmd line with & concatenated commands is prepared in parsing time (the same as a command block enclosed in parentheses) and then run entirely, indivisibly. Omit &exit /B 12345 to get proper exit code from anycommand, or replace it with something like start "" /W cmd /c "anycommand&&exit /B 12345||exit /B 54321" to get only success/failure indication.
Next code snippet could help:
#ECHO OFF
SETLOCAL enableextensions
set "_command=2nd_batch_file.bat"
:: for debugging purposes
set "_command=TIMEOUT /T 10 /NOBREAK"
:: raise errorlevel 9009 as a valid file name can't contain a vertical line
invalid^|command>nul 2>&1
echo before %errorlevel%
start "" /w %comspec% /C "mode 70,10&title Folder Confirmation Box&color 1e&echo(&echo( Else the close window&%_command%"
echo after %errorlevel%
Output shows sample %_command% exit codes: 0 or 1 if came to an end properly but -1073741510 if terminated forceably by Ctrl+C or Ctrl+Break or red ×
==>D:\bat\SO\31866091.bat<nul
before 9009
after 0
==>D:\bat\SO\31866091.bat<nul
before 9009
after 1
==>D:\bat\SO\31866091.bat<nul
before 9009
^CTerminate batch job (Y/N)?
after -1073741510
==>

This works for me:
call :runme start /w "Child Process" %comspec% /c "child.bat & exit 12345" <NUL >NUL 2>NUL
echo %ERRORLEVEL%
goto :eof
:runme
%*
goto :eof
The idea is to call a subroutine in the current script rather than calling out to an external script. You can still redirect input and output for a subroutine call.

Related

Execute multiple batch files concurrently and monitor if their process is completed

I have a main batch file which calls multiple batch files. I want to be able to execute all these batch files at the same time. Once they are all done, I have further processes that needs to carry on in the main batch file.
When I use 'Start' to call the multiple batch files, I'm able to kick off all batch files concurrently but I lose tracking of them. (Main batch file thinks their processes are done the moment it executes other batch files).
When I use 'Call', I'm able to monitor the batch file process, but it kicks off the batch files sequentially instead of concurrently.
Is there a way around this? I have limited permissions on this PC and I'm trying to accomplish this using Batch only.
Main Batch file
call first.bat
call second.bat
call third.bat
:: echo only after all batch process done
echo done!
first.bat
timeout /t 10
second.bat
timeout /t 10
third.bat
timeout /t 10
This is the simplest and most efficient way to solve this problem:
(
start first.bat
start second.bat
start third.bat
) | pause
echo done!
In this method the waiting state in the main file is event driven, so it does not consume any CPU time. The pause command would terminate when anyone of the commands in the ( block ) outputs a character, but start commands don't show any output in this cmd.exe. In this way, pause keeps waiting for a char until all processes started by start commands ends. At that point the pipe line associated to the ( block ) is closed, so the pause Stdin is closed and the command is terminated by cmd.exe.
This will generate a temporary file and lock it by creating a redirection to it, starting the batch subprocesses inside this redirection. When all the subprocesses end the redirection is closed and the temporary file is deleted.
While the subprocesses are running, the file is locked, and we can test this trying to rename the file. If we can rename the file, subprocesses have ended, else some of the processes are still running.
#echo off
setlocal enableextensions disabledelayedexpansion
for %%t in ("%temp%\%~nx0.%random%%random%%random%.tmp") do (
echo Starting subprocesses
9> "%%~ft" (
start "" cmd /c subprocess.bat
start "" cmd /c subprocess.bat
start "" cmd /c subprocess.bat
start "" cmd /c subprocess.bat
start "" cmd /c subprocess.bat
)
echo Waiting for subprocesses to end
break | >nul 2>nul (
for /l %%a in (0) do #(ren "%%~ft" "%%~nxt" && exit || ping -n 2 "")
)
echo Done
) & del "%%~ft"
note: any process started inside the subprocesses will also hold the redirection and the lock. If your code leaves something running, this can not be used.
#ECHO Off
SETLOCAL
:: set batchnames to run
SET "batches=first second third"
:: make a tempdir
:maketemp
SET /a tempnum=%random%
SET "tempdir=%temp%\%tempnum%"
IF EXIST "%tempdir%*" (GOTO maketemp) ELSE (MD "%tempdir%")
FOR %%a IN (%batches%) DO START "%%a" %%a "%tempdir%\%%a"
:wait
timeout /t 1 >nul
FOR %%a IN (%batches%) DO IF exist "%tempdir%\%%a" GOTO wait
RD "%tempdir%" /S /Q
GOTO :EOF
Where the batches are constructed like
#ECHO OFF
:: just delay for 5..14 seconds after creating a file "%1", then delete it and exit
SETLOCAL
ECHO.>"%~1"
SET /a timeout=5+(%RANDOM% %% 10)
timeout /t %timeout% >NUL
DEL /F /Q "%~1"
EXIT
That is, each called batch first creates a file in the temporary directory, then deletes it after the required process is run. The filename to create/delete is provided as the first parameter to the batch and "quoted" because the temp directoryname typically contains separators.
The mainline simply creates a temporary directory and invokes the subprocedures, then repeatedly waits 1 second and checks whether the subprocedures' flagfile have all been deleted. Only if they have all been deleted with the procedure continue to delete the temporary directory
Adding to the answer by Aacini. I was also looking for similar task. Objective was to run multiple commands parallel and extract output (stdout & error) of all parallel processes. Then wait for all parallel processes to finish and execute another command. Following is a sample code for BAT file, can be executed in CMD:
(
start "" /B cmd /c ping localhost -n 6 ^>nul
timeout /t 5 /nobreak
start "" /B /D "C:\users\username\Desktop" cmd /c dir ^> dr.txt ^2^>^&^1
start "" /B cmd /c ping localhost -n 11 ^>nul
timeout /t 10 /nobreak
) | pause
Echo waited
timeout /t 12 /nobreak
All the statements inside () are executed first, wait for them to complete, then last two lines are executed. All commands begining with start are executed simultaneously.

How to close cmd window with command?

I make a program on cmd with 2 windows and I want to close one cmd window with a command.
if %choise%==1 goto menu
:menu
cls
color a
echo ===============================
echo MENU
echo ===============================
echo 1.Eixt 2.History
echo.
set /p choise=Choose:
if %choise%==1 exit
if %choise%==2 goto History
can anyone help?
I want to close one cmd window with a command
rem if %choise%==1 goto menu
The above line will cause an error if %choise% is not defined so remove it.
goto was unexpected at this time.
Use the following batch file:
#echo off
:menu
cls
color a
echo ===============================
echo MENU
echo ===============================
echo 1.Exit 2.History
echo.
set /p choise=Choose:
if %choise%==1 exit
if %choise%==2 goto History
endlocal
If you press 1 the batch file will exit the cmd shell
If you press 2 you will get an error as there is no label history
The system cannot find the batch label specified - History
I have two windows, "MWprog" and "History Box"
I want to close the "History box" window without closing the "MWprog" windows.
Add the following command to the batch file:
taskkill /f /fi "WINDOWTITLE eq Administrator: History Box"
Note:
The extra space - there are two after the : - is required.
To exit a batch use goto :eof [End of File]
if "%ShouldIExit%"=="TRUE" goto :EOF
or
if "%ShouldIExit%"=="TRUE" exit
or
if "%ShouldIExit%"=="TRUE" exit 2
Rem exit 2 will set the errorlevel to 2, and exit the program.
or
Search for this program on your computer, usually deep in the windows directory.
TaskKill.exe

Execute batch code when user click on exit

I am working on some code testing, and I stumbled on a problem I can't find or fix. My problem is:
If a user accidentally closes the cmd window, I'd like to execute a batch code before it actually closes. For example:
I run script A.bat . When a user wants to exit, I want it to delete my B.bat and then close the window.
This is how the code may look like:
#ECHO OFF
echo Welcome to A.bat
del B.bat (when user exits the window)
I couldn't find it on google and forums, so I thought maybe you guys could help me out. Thanks in advance, Niels
This works for me:
#ECHO OFF
if "%1" equ "Restarted" goto %1
start "" /WAIT /B "%~F0" Restarted
del B.bat
goto :EOF
:Restarted
echo Welcome to A.bat
echo/
echo Press any key to end this program and delete B.bat file
echo (or just close this window via exit button)
pause
exit
EDIT: Some explanations added
The start command restart the same Batch file in a new cmd.exe session; the /B switch open it in the same window and the /WAIT switch makes the original file to wait until the new one ends. The new Batch file must end with exit in order to kill the new cmd.exe session (because it was started with the /K switch). No matters if the new cmd.exe session ends normally because the exit command or because it was cancelled with the red X; in any case the control returns after the line that started it in the original execution.
I've had to do something similar to what you're describing. I'm not sure whether this is the simplest or most efficient way to accomplish what you ask, but it does indeed work nevertheless.
#echo off
setlocal
:lockFile
rem // create lock file to inform forked helper thread when this thread completes
rem // credit to dbenham: http://stackoverflow.com/a/27756667/1683264
set "lockFile=%temp%\%~nx0_%time::=.%.lock"
9>&2 2>NUL (2>&9 8>"%lockFile%" call :main %*) || goto :lockFile
del "%lockFile%"
exit /b
:main
call :cleanup_watcher "B.bat"
rem // put your main script here
pause
goto :EOF
:cleanup_watcher <file> (<file> <file> etc.)
rem // Write external script to delete filename arguments
rem // (so if main script exits via ^C, temp files are still removed)
>"%temp%\tmp.bat" (
echo #echo off
echo setlocal
echo :begin
echo ping -n 1 -w 500 169.254.1.1 ^>NUL
echo del /q "%temp%\%~nx0*.lock" ^>NUL 2^>NUL
rem // If lockfile can't be deleted, the main script is still running.
echo if exist "%temp%\%~nx0*.lock" goto :begin
echo del /q "%temp%\tmp.bat" %* ^&^& exit
)
rem // fork cleanup watcher invisibly to catch ^C
>"%lockfile%.vbs" echo CreateObject("Wscript.Shell").Run "%temp%\tmp.bat", 0, False
wscript "%lockfile%.vbs"
del "%lockfile%.vbs"

"EXIT" in batch file cannot overwrite return code of previous commands

Running this batchfile from the Windows commandline results in an %errorlevel% of 5 (i.e. running echo %errorlevel% on the commandline after having executing the batch file prints the number 5):
EXIT /B 5
This is fine.
However, running this batchfile results in an %errorlevel% of 0, no matter what:
sleep 1
EXIT /B 5
I want it to return the error code 5. How can I do that?
Note: If I add sys.exit(13) to the sleep.py (see below), then the second batch file will return with an exit code of 13. So my batch file will return with the exit code of the sleep.py script instead of the exit code specified via the EXIT command (which is strange).
sleep.bat:
sleep.py %1
sleep.py:
import sys
import time
if len(sys.argv) == 2:
time.sleep(int(sys.argv[1]))
sleep is a batch file. When calling a batch file from another batch file this way, control never returns to the caller (akin to exec in POSIX). Use call sleep 1 instead.
Or just sleep with built-in programs:
:sleep1
setlocal
set /a X=%~1 + 1
ping ::1 -n %X% >nul 2>&1
endlocal
goto :eof
:sleep2
timeout /T %1 /nobreak
goto :eof

Windows Batch - CHOICE is useless after manually setting ERRORLEVEL

I ran into a problem in a bigger batch file I was making, and narrowed it down to a very particular problem. If I manually set the errorlevel like this: set errorlevel=5 , then the "choice" command can't set or override my errorlevel. How can I get past this from happening?
I made a batch file to test this out. Here it is:
#echo off
set errorlevel=5
choice /c 123
echo %errorlevel%
pause
And the output, if you were to press 2:
[1,2,3]?2
5
Press any key to continue . . .
I used to use a simple subroutine to set the errorlevel to any value:
#echo off
call :errorlevel=5
echo %errorlevel%
goto :EOF
:errorlevel
exit /B %1
use cmd /c exit /b 5 instead of set errorlevel=5
like this:
#echo off
cmd /c exit /b 5
choice /c 123
echo %errorlevel%
pause
System environment variables can be used by the batch file writer, but that is a really bad idea.
PATH TEMP WINDIR USERNAME USERPROFILE ERRORLEVEL TIME DATE are some of variable names you should avoid using. Type SET at a cmd prompt to see the usual ones that are in use, but it doesn't show them all.
Choice is operating normally, and other tools will fail to set an errorlevel too.

Resources