从 Windows bat 文件并行调用多个 .exe 文件并将结果传输到一个公共文件?
Calling several .exe-files in parallel from a Windows bat-file and piping the results to a common file?
我想要一个标准的 Windows .bat 文件并行调用一个 .exe 文件的 3 个实例,并将结果通过管道传输到一个通用文本文件。伪代码:
SET PATH_TO_OUTPUT_FILE=%~dp0"All Devices Contents.txt"
echo Device on COM1 returned the following: > %PATH_TO_OUTPUT_FILE%
start /b ReadFromComPortDevice.exe COM1 >> %PATH_TO_OUTPUT_FILE%
echo Device on COM2 returned the following: >> %PATH_TO_OUTPUT_FILE%
start /b ReadFromComPortDevice.exe COM2 >> %PATH_TO_OUTPUT_FILE%
echo Device on COM3 returned the following: >> %PATH_TO_OUTPUT_FILE%
start /b ReadFromComPortDevice.exe COM3 >> %PATH_TO_OUTPUT_FILE%
显然,上述操作会产生文件访问错误,因为该文件已被第一个进程使用。有人知道解决方法吗?
最简单的方法是将整个批处理文件的输出重定向到指定文件:
@Echo off & CD /D "%~dp0"
If not defined _ (
Set _=_
Prompt $_
Start /B "" "%~f0" >"All Devices Contents.txt"
(Prompt )
Exit /B
)
echo Device on COM1 returned the following:
start /b ReadFromComPortDevice.exe COM1
echo Device on COM2 returned the following:
start /b ReadFromComPortDevice.exe COM2
echo Device on COM3 returned the following:
start /b ReadFromComPortDevice.exe COM3
Set "_="
Goto :Eof
以下内容的顺序似乎是正确的:
echo Device on COM1 returned the following: > TempCom1.txt
start /b ReadFromComPortDevice.exe COM1 >> TempCom1.txt
echo Device on COM2 returned the following: > TempCom2.txt
start /b ReadFromComPortDevice.exe COM2 >> TempCom2.txt
echo Device on COM3 returned the following: > TempCom3.txt
start /b ReadFromComPortDevice.exe COM3 >> TempCom3.txt
call:waitForFileUnlocked TempCom1.txt
call:waitForFileUnlocked TempCom2.txt
call:waitForFileUnlocked TempCom3.txt
copy TempCom1.txt+TempCom2.txt+TempCom3.txt "All Devices Contents.txt"
call:waitForFileUnlocked TempCom1.txt
call:waitForFileUnlocked TempCom2.txt
call:waitForFileUnlocked TempCom3.txt
del "TempCom1.txt"
del "TempCom2.txt"
del "TempCom3.txt"
pause
:waitForFileUnlocked
@echo off
:Wait
2>nul (
>>%~1 echo off
) && (echo ) || goto :Wait
goto:eof
我想要一个标准的 Windows .bat 文件并行调用一个 .exe 文件的 3 个实例,并将结果通过管道传输到一个通用文本文件。伪代码:
SET PATH_TO_OUTPUT_FILE=%~dp0"All Devices Contents.txt"
echo Device on COM1 returned the following: > %PATH_TO_OUTPUT_FILE%
start /b ReadFromComPortDevice.exe COM1 >> %PATH_TO_OUTPUT_FILE%
echo Device on COM2 returned the following: >> %PATH_TO_OUTPUT_FILE%
start /b ReadFromComPortDevice.exe COM2 >> %PATH_TO_OUTPUT_FILE%
echo Device on COM3 returned the following: >> %PATH_TO_OUTPUT_FILE%
start /b ReadFromComPortDevice.exe COM3 >> %PATH_TO_OUTPUT_FILE%
显然,上述操作会产生文件访问错误,因为该文件已被第一个进程使用。有人知道解决方法吗?
最简单的方法是将整个批处理文件的输出重定向到指定文件:
@Echo off & CD /D "%~dp0"
If not defined _ (
Set _=_
Prompt $_
Start /B "" "%~f0" >"All Devices Contents.txt"
(Prompt )
Exit /B
)
echo Device on COM1 returned the following:
start /b ReadFromComPortDevice.exe COM1
echo Device on COM2 returned the following:
start /b ReadFromComPortDevice.exe COM2
echo Device on COM3 returned the following:
start /b ReadFromComPortDevice.exe COM3
Set "_="
Goto :Eof
以下内容的顺序似乎是正确的:
echo Device on COM1 returned the following: > TempCom1.txt
start /b ReadFromComPortDevice.exe COM1 >> TempCom1.txt
echo Device on COM2 returned the following: > TempCom2.txt
start /b ReadFromComPortDevice.exe COM2 >> TempCom2.txt
echo Device on COM3 returned the following: > TempCom3.txt
start /b ReadFromComPortDevice.exe COM3 >> TempCom3.txt
call:waitForFileUnlocked TempCom1.txt
call:waitForFileUnlocked TempCom2.txt
call:waitForFileUnlocked TempCom3.txt
copy TempCom1.txt+TempCom2.txt+TempCom3.txt "All Devices Contents.txt"
call:waitForFileUnlocked TempCom1.txt
call:waitForFileUnlocked TempCom2.txt
call:waitForFileUnlocked TempCom3.txt
del "TempCom1.txt"
del "TempCom2.txt"
del "TempCom3.txt"
pause
:waitForFileUnlocked
@echo off
:Wait
2>nul (
>>%~1 echo off
) && (echo ) || goto :Wait
goto:eof