我试图打开第二个批处理文件并检测它是否正常由用户退出或关闭(ctrl + c或x或window termiate等...) 所以我在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
我试图保持第一批等待(/ W),因为我稍后会检查错误级别 但在关闭第二批文件后,我得到一个错误,如^ cterminate批处理作业(是/否)?
我在https://superuser.com/questions/35698/how-to-supress-terminate-batch-job-y-n-confirmation
上尝试了这个建议使用脚本
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
)
这很好用 那么有没有办法从同一个批处理文件开始并避免终止? 或者我是否必须从同一批次创建新批次并调用它? (我不希望他们看到文件)
答案 0 :(得分:2)
这对我有用:
call :runme start /w "Child Process" %comspec% /c "child.bat & exit 12345" <NUL >NUL 2>NUL
echo %ERRORLEVEL%
goto :eof
:runme
%*
goto :eof
这个想法是在当前脚本中调用子例程而不是调用外部脚本。您仍然可以重定向输入和输出以进行子程序调用。
答案 1 :(得分:1)
errorlevel
using set
command. Doing that causes it becomes unvolatile in current context.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
==>