【问题标题】:Redirect echos of Windows batch-file commands重定向 Windows 批处理文件命令的回显
【发布时间】:2020-12-20 00:22:28
【问题描述】:

在执行.bat 文件(如果未指定echo OFF)时,Windows 命令提示符的默认行为是在运行脚本之前对脚本中的每个命令进行echo。例如,如果我创建一个带有内容的文件example.bat

ver
vol

然后运行脚本会在命令窗口中产生以下内容:

C:\test>example.bat

C:\test>ver

Microsoft Windows [Version 10.0.XXXXX.XXX]

C:\test>vol
 Volume in drive C is Local Disk
 Volume Serial Number is XXXX-XXXX

C:\test>

如果我更改脚本的内容以将其中一个命令重定向到文件,例如

ver>example.log
vol

然后命令(Microsoft Windows [Version 10.0.XXXXX.XXX])的输出被重定向到日志文件,但输入的回显C:\test>ver)仍然打印到命令提示符窗口和以前一样。有没有办法可以将回显的命令重定向到日志文件,就像重定向输出一样?

请注意,我仍然希望像以前一样将第二个命令 (vol) 的回显和输出打印到命令提示符窗口。因此,仅将批处理脚本本身的执行回显到日志文件并不能完成这项工作。

【问题讨论】:

    标签: batch-file echo command-prompt


    【解决方案1】:

    一种可能的方式如下:

    @rem /* The main section of the script here is not echoed due to `@`;
    @rem    the actual commands to be echoed are placed in a sub-routine,
    @rem    which is called here; the call is then redirected to a file: */
    @call :SUB > "file.log"
    @rem // This command and its output are echoed to the console:
    vol
    @rem // The script needs to be quit here not to execute the rest again:
    @exit /B
    
    :SUB
    @rem // This command is subject to the redirection of the call:
    ver
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 2012-07-14
      • 2012-12-10
      • 2011-02-11
      • 2011-09-10
      • 1970-01-01
      相关资源
      最近更新 更多