Windows批处理:'三通'命令 [英] Windows batch: 'tee' command

查看:133
本文介绍了Windows批处理:'三通'命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找Linux的 T恤命令在Windows批处理文件中使用,而无需使用PowerShell中的等价物。

I'm looking for an equivalent of the Linux tee command to use in a Windows batch file without using PowerShell.

修改

其实我一直在寻找一种天然的方法来做到这一点,看起来这是不可能的。

Actually I was looking for a native way to do it, looks like it's impossible.

推荐答案

注:我认为这是纯粹的批量解决方案是主要学术兴趣;我不认为这是一个非常有用的工具。有到纯批量溶液严重的局限性。如果你想有一个一般可用纯天然脚本解决方案,那么我强烈建议混合的JScript /批tee.bat 工具。

我已经成功地写一个纯粹的批量实现三通实用工具,异步(非阻塞)的。它仅使用内部批处理命令加上本地FIND外部命令。

I've managed to write a pure batch implementation of the tee utility that is asynchronous (non-blocking). It uses only internal batch commands plus the native FIND external command.

这一切都输出写入到%TEMP%文件夹中的临时文件,同时使用来自同一文件读取 SET / P 。该临时文件名包含了当前时间至0.1秒,脚本会自动循环回来,如果另一个进程发生抢同一个名字温度再试试。

It writes all output to a temporary file in the %TEMP% folder, and simultaneously reads from that same file using SET /P. The temp file name incorporates the current time to 0.1 seconds, and the script automatically loops back to try again if another process happens to grab the same temp name.

batchTee.bat 表现相当好,虽然有很多的Unix的发球实用性能更好的免费的Windows端口。还有一个混合的JScript /批tee.bat执行更好。但是,这是使用纯批来解决一个有趣的问题。

batchTee.bat performs quite well, though there are plenty of free Windows ports of the Unix tee utility that perform better. There is also a Hybrid JScript/batch tee.bat that performs better. But this was an interesting problem to solve using pure batch.

有是在文档中的脚本的顶部列出了一些严重的局限性。

There are a number of serious limitations that are listed in the documentation at the top of the script.

batchTee.bat

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

用法是你所期望的:

Usage is as you would expect:

dir | batchTee output.txt

以上将覆盖任何现有output.txt的。

The above will overwrite any existing output.txt.

内容可以通过添加 + 第二个参数被附加到现有文件。

Content can be appended to an existing file by adding a + second argument.

dir | batchTee output.txt +

这篇关于Windows批处理:'三通'命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆