如何将windows命令输出重定向到控制台和文件

如何将windows命令输出重定向到控制台和文件,windows,command-line,Windows,Command Line,在windows中,我需要发出与以下命令类似的命令 c:>dir > file.txt 但是我需要file.txt和控制台上的输出。有什么办法可以做到这一点。我们就重复一下怎么样 dir > file.txt & type file.txt 语法为:dir | batchtee file.txt 这是一个由dbenham设计的批处理文件工具 ::batchTee.bat OutputFile [+] :: :: Write each line of std

在windows中,我需要发出与以下命令类似的命令

  c:>dir > file.txt

但是我需要file.txt和控制台上的输出。有什么办法可以做到这一点。

我们就重复一下怎么样

dir > file.txt & type file.txt

语法为:dir | batchtee file.txt

这是一个由dbenham设计的批处理文件工具

::batchTee.bat  OutputFile  [+]
::
::  Write each line of stdin to both stdout and outputFile.
::  The default behavior is to overwrite any existing outputFile.
::  If the 2nd argument is + then the content is appended to any existing
::  outputFile.
::
::  Limitations:
::
::  1) Lines are limited to ~1000 bytes. The exact maximum line length varies
::     depending on the line number. The SET /P command is limited to reading
::     1021 bytes per line, and each line is prefixed with the line number when
::     it is read.
::
::  2) Trailing control characters are stripped from each line.
::
::  3) Lines will not appear on the console until a newline is issued, or
::     when the input is exhaused. This can be a problem if the left side of
::     the pipe issues a prompt and then waits for user input on the same line.
::     The prompt will not appear until after the input is provided.
::

@echo off
setlocal enableDelayedExpansion
if "%~1" equ ":tee" goto :tee

:lock
set "teeTemp=%temp%\tee%time::=_%"
2>nul (
  9>"%teeTemp%.lock" (
    for %%F in ("%teeTemp%.test") do (
      set "yes="
      pushd "%temp%"
      copy /y nul "%%~nxF" >nul
      for /f "tokens=2 delims=(/" %%A in (
        '^<nul copy /-y nul "%%~nxF"'
      ) do if not defined yes set "yes=%%A"
      popd
    )
    for /f %%A in ("!yes!") do (
        find /n /v ""
         echo :END
         echo %%A
      ) >"%teeTemp%.tmp" | <"%teeTemp%.tmp" "%~f0" :tee %* 7>&1 >nul
    (call )
  ) || goto :lock
)
del "%teeTemp%.lock" "%teeTemp%.tmp" "%teeTemp%.test"
exit /b

:tee
set "redirect=>"
if "%~3" equ "+" set "redirect=>>"
8%redirect% %2 (call :tee2)
set "redirect="
(echo ERROR: %~nx0 unable to open %2)>&7

:tee2
for /l %%. in () do (
  set "ln="
  set /p "ln="
  if defined ln (
    if "!ln:~0,4!" equ ":END" exit
    set "ln=!ln:*]=!"
    (echo(!ln!)>&7
    if defined redirect (echo(!ln!)>&8
  )
)
::batchTee.bat输出文件[+]
::
::将stdin的每一行写入stdout和outputFile。
::默认行为是覆盖任何现有的outputFile。
::如果第2个参数为+,则内容将附加到任何现有参数
::输出文件。
::
::限制:
::
::1)行限制为~1000字节。确切的最大线长度各不相同
::取决于行号。SET/P命令仅限于读取
::每行1021字节,并且每行在
当前位置阅读。
::
::2)从每行中删除尾随控制字符。
::
::3)在发出换行符之前,控制台上不会显示行,或者
::使用输入时。这可能是一个问题,如果
::管道发出提示,然后在同一行上等待用户输入。
::在提供输入之前,不会出现提示。
::
@回音
setlocal enableDelayedExpansion
如果“%~1”eq:tee”转到:tee
:锁
设置“teeTemp=%temp%\tee%时间::=\uu3%”
2> 努尔(
9> %teeTemp%.lock(
对于%%F英寸(“%teeTemp%”测试),请执行以下操作(
设置“是=”
pushd“%temp%”
复制/y nul“%%~nxF”>nul
对于/f“令牌=2 delims=(/”%%A in(

“^您可以创建一个程序来执行此工作。它会将其标准输入回显到标准输出和文件中指定为它的参数。然后将任何命令的输出通过管道传输到程序中。这可能适用于像
dir
这样的简单命令,但如果您有一个长脚本,需要做大量工作,则只有在脚本完成后,您才能在执行期间在控制台上看到输出。