我通过批处理文件调用C#控制台应用程序,以便将应用程序输出发送到文本文件中,包含日期/时间等。
我遇到的问题是,当控制台应用程序完成时,它会打开批处理窗口,因为有一个PAUSE(等效的C#),因此必须按一个键才能关闭窗口。这意味着我不知道工作什么时候结束。
有没有办法在应用程序完成后关闭CMD窗口,而不必更改C#应用程序代码?
@ECHO================================================================================
@ECHO The Application is currently running and may take some time. Please wait...
@ECHO================================================================================
@ECHO OFF
C:\Applications\Job\Job.exe > C:\Applications\Job\Job_Output\"Output_%date:/=-% %time::=-%.txt"
答案 0 :(得分:1)
试试这个(注意echo
之后的整理点):
echo.| C:\Applications\Job\Job.exe > C:\Applications\Job\Job_Output\"Output_%date:/=-% %time::=-%.txt"
我尝试使用pause
并且效果很好:
echo.| pause
echo.
不是echo
。它只是打印一个换行符,就是触发暂停所需的内容。
答案 1 :(得分:0)
如果您的控制台应用已经有Console.ReadLine()
或Console.ReadKey()
方法但不是仅仅调用*.exe
而是使用将运行的Start
命令,则不确定是否会有效可执行文件在单独的窗口中,如
start "MyConsoleTask" C:\Applications\Job\Job.exe > C:\Applications\Job\Job_Output\"Output_%date:/=-% %time::=-%.txt"
答案 2 :(得分:0)
如果您无法访问控制台应用源代码,可以尝试解决方法
@echo off
@echo================================================================================
@echo The Application is currently running and may take some time. Please wait...
@echo================================================================================
set "timeStamp=%date:/=-%_%time::=-%
set "timeStamp=%timeStamp:~0,-3%" & rem remove ,centiseconds.
set "logFile=C:\Applications\Job\Job_Output\Output_%timeStamp%.txt"
rem start the exe in the same cmd window
start /B "" """C:\Applications\Job\Job.exe" > "%logFile%"""
rem wait for process startup
ping 1.1.1.1 -n 1 -w 750 >NUL
rem wait for logFile to be closed. This may flag that job.exe has ended
:wait
ping 1.1.1.1 -n 1 -w 50 >NUL & rem this avoids processor load
2>nul (>>"%logFile%" call )||goto :wait
rem send a key to the console. This may be captured by the exe file
set "_vbs_file_=%TEMP%\sendConsole.vbs"
(
echo/ set oWS ^= CreateObject^("wScript.Shell"^)
echo/ wScript.Sleep 50
echo/ oWS.SendKeys "{ENTER}"
)>"%_vbs_file_%"
if exist "%TEMP%\sendConsole.vbs" (set "_spawn_=%TEMP%\sendConsole.vbs") else (set "_spawn_=sendConsole.vbs")
ping 1.1.1.1 -n 1 -w 50 >NUL
start /B /WAIT cmd /C "cls & "%_spawn_%" & del /F /Q "%_spawn_%" 2>NUL"
@echo================================================================================
@echo Process completed. I guess...
@echo================================================================================
exit/B
所以,
start /B ...
在同一cmd窗口中启动 job.exe可执行文件。
:wait
ping 1.1.1.1 -n 1 -w 50 >NUL & rem this avoids processor load
2>nul (>>"%logFile%" call )||goto :wait
等待 logfile 关闭,因此可能表示上一个进程已经结束。
set "_vbs_file_=%TEMP%\sendConsole.vbs"
(
echo/ set oWS ^= CreateObject^("wScript.Shell"^)
echo/ wScript.Sleep 50
echo/ oWS.SendKeys "{ENTER}"
)>"%_vbs_file_%"
if exist "%TEMP%\sendConsole.vbs" (set "_spawn_=%TEMP%\sendConsole.vbs") else (set "_spawn_=sendConsole.vbs")
ping 1.1.1.1 -n 1 -w 50 >NUL
start /B /WAIT cmd /C "cls & "%_spawn_%" & del /F /Q "%_spawn_%" 2>NUL"
将回车键发送到控制台,因此等待击键的过程可能会捕获它。
注意:仅当IP无法访问时, ping等待技巧才能正常工作。
注意:讨论了呼叫和/或转到技巧here
答案 3 :(得分:0)
我们必须在这里模拟按键,因此我们应该使用键盘缓冲器。
我不是批处理专家,这是我找到如何按批次按键的答案:
@if (@CodeSection == @Batch) @then
@echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem Open the command here
start "" /B Job.exe > JobOutput.txt
rem sends the keys composing the string "I PRESSED " and the enter key
%SendKeys% "I PRESSED {ENTER}"
goto :EOF
@end
// JScript section
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
源: