【问题标题】:COPY command - unexpected results in STDERR redirection inside a .BAT fileCOPY 命令 - .BAT 文件内的 STDERR 重定向出现意外结果
【发布时间】:2020-02-15 22:12:06
【问题描述】:

我正在尝试使用 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)

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

【问题讨论】:

  • 不幸的是,Copy 不会将该消息输出为 StdErr。以前XCopy 是作为替代方案提供的。请参阅this question 了解更多信息。
  • @Compo - 感谢您的及时解释。我还想对您的答案和 John Rocha 的答案都投赞成票,并接受两者,但在此布局中看不到任何内容。再次感谢!

标签: batch-file cmd error-handling copy stderr


【解决方案1】:

很遗憾,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.

您至少可以看到失败的是您的 2nd 和 4th 复制命令。

【讨论】:

  • 感谢您,我今天学到了另一种新方法。此行号可以帮助我调查错误。此外,我对 XCOPY 进行了如下试验,它产生了另一个可能的错误外部参照。 .BAT 内容:echo F | XCopy Source\FileExist01.txt Dest\NewFile01.txt /Y >XCopy.log 2>XCopyError.log' 'echo F | XCopy Source\NoFile02.txt Dest\NewFile02.txt /Y >>XCopy.log 2>>XCopyError.logXCopyError.log 显示 File not found - NoFile02.txt 谢谢!
  • 好吧XCopy,我建议你使用这个命令行XCopy "Source\FileExist01.txt" "Dest\NewFile01.txt*" /Y>"XCopy.log"星号将抑制File或Dictory提示。
【解决方案2】:

这是因为复制命令不会将该错误消息打印到标准错误。我知道,WTF!?

这里有一些快速测试供您确认。从命令行尝试以下操作:

dir missing-file.txt

观察

>dir missing-file.txt
 Volume in drive C has no label.
 Volume Serial Number is EC0D-D428

 Directory of c:\TEMP

File Not Found

接下来进行重定向并观察它是否有效,错误消息放在 elog.txt 文件中。

>dir missing-file.txt
 Volume in drive C has no label.
 Volume Serial Number is EC0D-D428

 Directory of 

File Not Found

>dir missing-file.txt 2> elog.txt
 Volume in drive C has no label.
 Volume Serial Number is EC0D-D428

 Directory of 


>type elog.txt
File Not Found

现在,重复上述操作,但这次使用复制,然后作为最后一个实验,重定向标准输出 (1) 并观察消息是否被重定向。显示该副本会将其错误消息放在标准输出上。

>copy /y missing-file.txt n
The system cannot find the file specified.

>copy /y missing-file.txt n 2> elog.txt
The system cannot find the file specified.

>copy /y missing-file.txt n 1> elog.txt

>type elog.txt
The system cannot find the file specified.

【讨论】:

  • 我知道正确的WTF。谢谢你的详细建议。我还用 XCOPY 弄乱了预期的结果。非常感谢!
猜你喜欢
  • 2019-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多