牛魔王的故事
是的,有一种方法可以在控制台(屏幕)和文件中显示单个命令输出。使用您的示例,使用...@ECHO OFFFOR /F "tokens=*" %%I IN ('DIR') DO ECHO %%I & ECHO %%I>>windows-dir.txt详细说明:该FOR命令将命令或文本的输出解析为变量,可以多次引用该变量。对于诸如的命令,请DIR /B用单引号引起来,如下例所示。将DIR /B文本替换为所需的命令。FOR /F "tokens=*" %%I IN ('DIR /B') DO ECHO %%I & ECHO %%I>>FILE.TXT要显示文本,请用双引号引起来,如下例所示。FOR /F "tokens=*" %%I IN ("Find this text on console (screen) and in file") DO ECHO %%I & ECHO %%I>>FILE.TXT...以及换行...FOR /F "tokens=*" %%I IN ("Find this text on console (screen) and in file") DO ( ECHO %%I & ECHO %%I>>FILE.TXT)如果您有时只希望在控制台(屏幕)上输出,而有时只希望将输出发送到文件,而有时又希望同时发送给两个文件,请使用变量指定FOR循环的“ DO”子句,如下所示%TOECHOWHERE%。@ECHO OFFFOR %%I IN (TRUE FALSE) DO ( FOR %%J IN (TRUE FALSE) DO ( SET TOSCREEN=%%I & SET TOFILE=%%J & CALL :Runit))GOTO :Finish:Runit REM Both TOSCREEN and TOFILE get assigned a trailing space in the FOR loops REM above when the FOR loops are evaluating the first item in the list, REM "TRUE". So, the first value of TOSCREEN is "TRUE " (with a trailing REM space), the second value is "FALSE" (no trailing or leading space). REM Adding the ": =" text after "TOSCREEN" tells the command processor to REM remove all spaces from the value in the "TOSCREEN" variable. IF "%TOSCREEN: =%"=="TRUE" ( IF "%TOFILE: =%"=="TRUE" ( SET TEXT=On screen, and in "FILE.TXT" SET TOECHOWHERE="ECHO %%I & ECHO %%I>>FILE.TXT" ) ELSE ( SET TEXT=On screen, not in "FILE.TXT" SET TOECHOWHERE="ECHO %%I" ) ) ELSE ( IF "%TOFILE: =%"=="TRUE" ( SET TEXT=Not on screen, but in "FILE.TXT" SET TOECHOWHERE="ECHO %%I>>FILE.txt" ) ELSE ( SET TEXT=Not on screen, nor in "FILE.TXT" SET TOECHOWHERE="ECHO %%I>NUL" ) ) FOR /F "tokens=*" %%I IN ("%TEXT%") DO %TOECHOWHERE:~1,-1%GOTO :eof:Finish ECHO Finished [this text to console (screen) only]. PAUSE