![](/img/trans.png)
[英]Batch Scripting - Saving the output of systeminfo command to a variable
[英]Command output to a file with batch scripting
我正在嘗試生成一個xml文件。 我使用返回數字的命令比較兩個圖像。 但是當我嘗試將其輸出重定向到文件時,它會使用換行符打印該數字。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.