从批处理文件中重定向输出

时间:2013-12-10 01:09:51

标签: windows batch-file cmd io-redirection

我正在创建一个包含一些简单命令的批处理文件,以便从系统中收集信息。批处理文件包含获取时间,IP信息,用户等的命令。

我在批处理文件中组装了所有命令,并且它运行,但是我希望批处理文件在运行时将结果输出到文本文件(日志)。是否有可以添加到批处理中的命令?

请记住,我不想从cmd运行批处理,然后重定向输出;如果可能的话,我想从批处理内部重定向输出。

10 个答案:

答案 0 :(得分:128)

这种简单的天真方式很慢,因为它会打开并多次将文件指针定位到文件结尾。

@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

答案 1 :(得分:50)

如果你想要重定向out和err流

dir >> a.txt 2>&1

答案 2 :(得分:14)

我知道这是一篇较旧的帖子,但有人会在谷歌搜索中偶然发现它,看起来OP在评论中提出的一些问题并未得到明确解决。另外,请放轻松,因为这是我在SO上发布的第一个答案。 :)

要使用动态生成的文件名将输出重定向到文件,我的首选(读取:快速和简单)方法是@dbenham提供的第二种解决方案。例如,这个:

@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
)

将创建一个类似于您在此screenshot of the file in the target directory

中看到的文件

那将包含此输出:

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

另请注意,此解决方案依赖于区域设置,因此请谨慎使用它。

答案 3 :(得分:9)

@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

答案 4 :(得分:8)

echo some output >"your logfile"

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

应填写账单。

如果您想APPEND输出,请使用>>代替>>将启动一个新的日志文件。

答案 5 :(得分:3)

您可以使用一个很酷的小程序将输出重定向到文件和控制台

some_command  ^|  TEE.BAT  [ -a ]  filename 



@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




答案 6 :(得分:2)

@echo OFF
[your command] >> [Your log file name].txt

我在批处理文件中使用了上面的命令,它可以正常工作。在日志文件中,它显示了我的命令的结果。

答案 7 :(得分:1)

在批处理文件顶部附近添加这两行,之后的所有stdout和stderr都将重定向到log.txt:

if not "%1"=="STDOUT_TO_FILE"  %0 STDOUT_TO_FILE %*  >log.txt 2>&1
shift /1

答案 8 :(得分:1)

在批处理文件底部添加以下几行,即可抓取CMD窗口中显示的所有内容并导出到文本文件中:

powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^a')
powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^c')
powershell Get-Clipboard > MyLog.txt

它基本上执行全选-> 复制到剪贴板-> 粘贴到文本文件中。

答案 9 :(得分:0)

在输入中出现“有毒”字符的情况下,这可能会失败。 考虑这样的输入IsAnIn ^^^^ put是一种很好的方式来了解正在发生的事情。 当然,有一个规则,输入字符串必须在双引号中,但是我有一个感觉,只有输入的含义是NTFS分区上的某个位置,该规则才是有效的规则(也许这是URL的规则)不确定)。 但这当然不是任意输入字符串的规则(这是“一种很好的做法”,但是您不能指望它)。