【问题标题】:Echo into multiple files in Batch script在批处理脚本中回显到多个文件
【发布时间】:2011-11-30 18:37:10
【问题描述】:

如何在单个命令中将一行回显到多个文件中? 示例:echo Hello World! 放入 file1.log 和 file2.log

编辑:Windows-XP 批处理脚本

挑战:给我一个单线 =D。如果不能用一行来完成,我不感兴趣。

我的解决方案是

ECHO Hello World!>tmp.log & COPY file1.log + tmp.log & COPY file2.log + tmp.log

但我希望一个是单个命令而不是多个命令。

【问题讨论】:

    标签: batch-file windows-xp cmd command-line-interface


    【解决方案1】:

    如果您只需要单行(输入),您可以使用批处理编写自己的 tee。

    @ECHO OFF
    rem *** singleLineTee destination1 destination2
    setlocal EnableDelayedExpansion
    set /p var=
    > %1 echo(!var!
    > %2 echo(!var!
    

    并与echo hello | singleLineTee file1.log file2.log一起使用

    编辑:与单衬里相同

    echo hello | ( cmd /v:on /c "set /p var=& >> file1.log echo !var!& >> file2.log echo !var!")
    

    cmd /v:on /c 是启用延迟扩展所必需的

    【讨论】:

    • 已编辑帖子:Windows-XP 需要它
    • tee 不是内部或外部命令、可运行程序或批处理文件。
    • 显然,你应该把上面的代码放到一个名为singleLineTee.bat的批处理中
    【解决方案2】:
    echo "Hello World" | tee file1.log file2.log
    

    【讨论】:

    • @Mechaflash 抱歉,我将“batch”读为“bash”并认为是 linux。
    【解决方案3】:

    您可以执行类似于 jeb 的操作,但将其保存在与调用代码相同的批处理文件中

    @ECHO OFF
        del tem1.txt
        del tem2.txt
        call :SingleLineTeeSub "echo Hello" Tem1.txt Tem2.txt
        call :SingleLineTeeSub "echo World" Tem1.txt Tem2.txt
        call :SingleLineTeeSub "Dir tem1.txt" Tem1.txt Tem2.txt
    goto :eof
    
    :SingleLineTeeSub
        rem *** call :SingleLineTeeSub "command [params]" destination1 destination2
        setlocal EnableDelayedExpansion
        set theCmd=%~1
        >> %2 %theCmd%
        >> %3 %theCmd%
    goto :eof
    

    【讨论】:

      【解决方案4】:

      这是否符合您的要求:

      (echo hello > file1) && copy file1 file2
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-05
        相关资源
        最近更新 更多