从batch file中redirect输出

我正在用一些简单的命令创build一个batch file,以从系统中收集信息,这个batch file包含获取时间的命令,IP信息,用户等等。我把所有的命令汇编在一个batch file中,运行,但我想batch file,当运行输出结果到一个文本文件(日志),是否有一个命令,我可以添加到批处理,这样做。 请记住,我不想从cmd运行批处理,然后redirect输出,我想redirect从批处理内输出,如果这是可能的。

简单朴素的方式(效率较低)

@echo off command1 >output.txt command2 >>output.txt ... commandN >>output.txt 

更好的方法 – 更容易编写,速度更快。

 @echo off >output.txt ( command1 command2 ... commandN ) 

另一个好方法

 @echo off call :sub >output.txt exit /b :sub command1 command2 ... commandN 

如果你想同时出错和错误的streamredirect

 dir >> a.txt 2>&1 

我知道这是一个较旧的post,但有人会在Googlesearch中绊倒它,而且看起来OP在注释中提出的一些问题没有具体解决。 另外,请容易对我,因为这是我的第一个答复张贴在SO。 🙂

要使用dynamic生成的文件名将输出redirect到文件,我的转到(read:quick&dirty)方法是@dbenham提供的第二个解决scheme。 举个例子,这个:

 @echo off > filename_prefix-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log ( echo Your Name Here echo Beginning Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log REM do some stuff here echo Your Name Here echo Ending Date/Time: %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log ) 

将创build一个文件,就像您在目标目录中的该文件的屏幕截图中看到的一样

这将包含这个输出:

 Your Name Here Beginning Date/Time: 2016-09-16_141048.log Your Name Here Ending Date/Time: 2016-09-16_141048.log 

另外请记住,这个解决scheme是依赖于语言环境的,所以在使用时要小心。

 @echo off >output.txt ( echo Checking your system infor, Please wating... systeminfo | findstr /c:"Host Name" systeminfo | findstr /c:"Domain" ipconfig /all | find "Physical Address" ipconfig | find "IPv4" ipconfig | find "Default Gateway" ) @pause 
 echo some output >"your logfile" 

要么

 ( echo some output echo more output )>"Your logfile" 

应该填写账单。

如果你想APPEND输出,使用>>而不是>>会启动一个新的日志文件。

有一个很酷的小程序可以用来将输出redirect到文件和控制台

some_command ^ | TEE.BAT [-a]文件名

  @ECHO OFF :: Check Windows version IF NOT "%OS%"=="Windows_NT" GOTO Syntax :: Keep variables local SETLOCAL :: Check command line arguments SET Append=0 IF /I [%1]==[-a] ( SET Append=1 SHIFT ) IF [%1]==[] GOTO Syntax IF NOT [%2]==[] GOTO Syntax :: Test for invalid wildcards SET Counter=0 FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA" IF %Counter% GTR 1 ( SET Counter= GOTO Syntax ) :: A valid filename seems to have been specified SET File=%1 :: Check if a directory with the specified name exists DIR /AD %File% >NUL 2>NUL IF NOT ERRORLEVEL 1 ( SET File= GOTO Syntax ) :: Specify /Y switch for Windows 2000 / XP COPY command SET Y= VER | FIND "Windows NT" > NUL IF ERRORLEVEL 1 SET Y=/Y :: Flush existing file or create new one if -a wasn't specified IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1) :: Actual TEE FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO ( > CON ECHO.%%B >> %File% ECHO.%%B ) :: Done ENDLOCAL GOTO:EOF :Count SET /A Counter += 1 SET File=%1 GOTO:EOF :Syntax ECHO. ECHO Tee.bat, Version 2.11a for Windows NT 4 / 2000 / XP ECHO Display text on screen and redirect it to a file simultaneously ECHO. IF NOT "%OS%"=="Windows_NT" ECHO Usage: some_command ³ TEE.BAT [ -a ] filename IF NOT "%OS%"=="Windows_NT" GOTO Skip ECHO Usage: some_command ^| TEE.BAT [ -a ] filename :Skip ECHO. ECHO Where: "some_command" is the command whose output should be redirected ECHO "filename" is the file the output should be redirected to ECHO -a appends the output of the command to the file, ECHO rather than overwriting the file ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com ECHO Modified by Kees Couprie ECHO http://kees.couprie.org ECHO and Andrew Cameron