使用批处理脚本将命令输出到文件
Command output to a file with batch scripting
我正在尝试生成一个 xml 文件。我使用 returns 一个数字的命令比较两个图像。但是当我尝试将其输出重定向到一个文件时,它会打印带有换行符的数字。
echo a.jpg >> "result.txt"
compare -metric NCC "a.jpg" "b.jpg" "c.jpg" 2>> "result.txt"
预期输出如下:
a.jpg 1
但它输出:
a.jpg
1
我试图从命令中获取结果并尝试与 a.jpg 连接,但我无法做到。
for /f "tokens=1 delims=" %%a in ('compare -metric NCC "a.jpg" "b.jpg" "c.jpg"') do set result=%%a
echo %result%
REM outputs 1ECHO is off.
第一个命令添加一个换行符。像这样使用它可以避免它并在一行中得到输出。
echo|set /p=a.jpg >> "result.txt"
现在我知道了,会发生什么:
compare -metric NCC "a.jpg" "b.jpg" "c.jpg" 2>> "result.txt"
你想要的输出是在 STDERR 上,而不是在 STDOUT 上(非常不寻常)。但是 for
只捕获 STDOUT。
应该可以调整 for
结构,但使用起来更简单:
<nul set /p "=a.jpg " >> "result.txt"
REM this line writes a string without linefeed
compare -metric NCC "a.jpg" "b.jpg" "c.jpg" 2>> "result.txt"
REM this line appends the STDERR of the "compare" command to the line
我正在尝试生成一个 xml 文件。我使用 returns 一个数字的命令比较两个图像。但是当我尝试将其输出重定向到一个文件时,它会打印带有换行符的数字。
echo a.jpg >> "result.txt"
compare -metric NCC "a.jpg" "b.jpg" "c.jpg" 2>> "result.txt"
预期输出如下:
a.jpg 1
但它输出:
a.jpg
1
我试图从命令中获取结果并尝试与 a.jpg 连接,但我无法做到。
for /f "tokens=1 delims=" %%a in ('compare -metric NCC "a.jpg" "b.jpg" "c.jpg"') do set result=%%a
echo %result%
REM outputs 1ECHO is off.
第一个命令添加一个换行符。像这样使用它可以避免它并在一行中得到输出。
echo|set /p=a.jpg >> "result.txt"
现在我知道了,会发生什么:
compare -metric NCC "a.jpg" "b.jpg" "c.jpg" 2>> "result.txt"
你想要的输出是在 STDERR 上,而不是在 STDOUT 上(非常不寻常)。但是 for
只捕获 STDOUT。
应该可以调整 for
结构,但使用起来更简单:
<nul set /p "=a.jpg " >> "result.txt"
REM this line writes a string without linefeed
compare -metric NCC "a.jpg" "b.jpg" "c.jpg" 2>> "result.txt"
REM this line appends the STDERR of the "compare" command to the line