batch-file - COPY命令-.BAT文件中的STDERR重定向导致意外结果

标签 batch-file cmd error-handling copy stderr

我正在尝试使用以下COPY文件中的 STDERR 命令和重定向 .BAT 句柄记录每次文件传输:

Copy /Y FileExist01.txt NewFile01.txt 2>CopyError.log
Copy /Y NoFile02.txt NewFile02.txt 2>>CopyError.log
Copy /Y FileExist03.txt NewFile03.txt 2>>CopyError.log
Copy /Y NoFile04.txt NewFile04.txt 2>>CopyError.log
  • FileExist##.txt 是我知道存在的文件(已验证路径和
    文件名)
  • NoFile##.txt 是我知道不存在的文件,无法测试
    出现错误时STDERR重定向(2>>CopyError.log)

  • 我期望在 2 中看到CopyError.log错误行以显示"The system cannot find the path specified.",但是 CopyError.log 为空。

    最佳答案

    不幸的是,Copy不会将该消息输出为StdErr

    以前提供XCopy作为替代方案。有关更多信息,请参见this question,但这是一个快速的想法:

    (   Copy /Y "FileExist01.txt" "NewFile01.txt"
        Copy /Y "NoFile02.txt" "NewFile02.txt"
        Copy /Y "FileExist03.txt" "NewFile03.txt"
        Copy /Y "NoFile04.txt" "NewFile04.txt"
    )|FindStr /VRC:"^ ">"CopyError.log"
    

    现在,与您的预期方法一样,这不会告诉您哪些命令实际输出了消息。如果您想这样做,我想可以输出行号:

    (   Copy /Y "FileExist01.txt" "NewFile01.txt"
        Copy /Y "NoFile02.txt" "NewFile02.txt"
        Copy /Y "FileExist03.txt" "NewFile03.txt"
        Copy /Y "NoFile04.txt" "NewFile04.txt"
    )|FindStr /VRNC:"^ ">"CopyError.log"
    

    在这种情况下,错误应以数字开头,在这种情况下:

    2:The system cannot find the file specified.
    4:The system cannot find the file specified.
    

    至少,您至少可以看到是您的24复制命令失败了。

    关于batch-file - COPY命令-.BAT文件中的STDERR重定向导致意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58458886/

    相关文章:

    windows - 将 Subversion (SVN) 符号链接(symbolic link)文本文件转换为 NTFS 符号链接(symbolic link)的批处理脚本

    batch-file - 批处理脚本 "for/f ..."

    batch-file - 批处理文件查找包含子字符串的文件名

    batch-file - 使用默认浏览器批量打开网页

    javascript - 如何用 "ReferenceError"捕获 "onerror"

    windows cmd批处理文件计算

    windows - 在windows批处理文件中获取环境变量

    c# - 从 C# 执行命令并等待完成执行

    sql - SQL SERVER 中的分组依据/打印相关查询

    backbone.js - Backbone 收集保存错误