I know I can use the FOR /F to get the command process output, as follows:

FOR /F %%I IN ('myprogram.cmd') DO (
  do something with %%I
)

However, above method I have to wait until "myprogram.cmd" is finished then get the entire output. I want to get the output in real time when "myprogram.cmd" is running.

Is it possible to use windows script only?

有帮助吗?

解决方案

Windows programs most certainly can process the output of another process asynchronously using pipes. Scripting languages like VBScript, JScript, and PowerShell should be able to do this easily. You simply pipe your output into your script, and the script reads from stdin within a loop, terminating when end of file is reached.

But doing so using pure batch is quite difficult and has significant limitations. It requires multiple batch processes, and a temporary file. The first batch process uses FIND to read from stdin and redirects the output to the temp file. The second batch process reads from the temp file using SET /P. See my batchTee.bat for an example of how this could be done.

But I don't recommend doing this except as an academic excercise. Instead you should use one of the other scripting languages that can do this easily.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top