我正在编写一个批处理脚本,它将循环遍历文本文件的每一行(每行包含一个文件名),检查文件是否存在,然后运行该文件并移动它.
这是我的批处理脚本:
REM Loop through each line of input.txt
FOR /F "tokens=1-3 delims=, " %%i IN (./ready/input.txt) DO (
ECHO.
ECHO.
ECHO.
ECHO Check %%i exists, set error flag if it doesnt
if not exist .\ready\%%i set errorlevel=2
echo return code is %errorlevel%
ECHO Run %%i if it exists
if errorlevel 0 call .\ready\%%i
ECHO Move %%i to archive if no error occured
if errorlevel 0 copy .\ready\%%i .\archive\%mydate%_%mytime%_%%j_%%k_%%i
ECHO Copy line of text to the new output.txt file if …Run Code Online (Sandbox Code Playgroud) 我有一个控制台应用程序.与此应用程序的交互是通过TCP/IP完成的.
我也有一个测试框架,它基本上是BATCH脚本的集合(...不是我的错).这个测试框架对每个测试的作用基本上是这样的:
start /min "myapplication.exe" 并等到收到应用程序启动并运行的验证.我目前遇到的一个问题是,由于某些内部错误,应用程序会过早退出.我想区分失败的测试和应用程序崩溃.我对此的唯一指示是应用程序的退出代码.
所以,我尝试了以下内容:
start /min cmd /c "myapplication.exe || echo %errorLevel% > exitcode.txt"
然后在测试脚本中,
if exist exitcode.txt (
set /p exitcode=<exitcode.txt
echo ERROR: myapplication.exe returned exitcode %exitcode%.
goto error
) else (
goto do_processing
)
Run Code Online (Sandbox Code Playgroud)
但由于一些奇怪的原因,文本文件永远不会出现,即使我有时会得到关于应用程序崩溃的对话框,即使我强行使用已知的非零退出代码使其失败.测试刚刚结束do_processing,(当然)导致失败.
编辑 当我跑
start /min cmd /c "nonsense || echo %errorLevel% > test.txt"
我有时会得到一个包含字符串9009的文本文件,但有时候该文本文件包含字符串0,或者有时候1,......有什么......?!
EDIT2 如果输入
cmd /k "nonsense || echo %errorLevel%"
(注意/k选项),你看到0在新窗口中打印,但如果你打字echo %errorlevel%,你得到1 …
windows batch-file command-prompt exit-code console-application