我正在尝试使用以下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.
至少,您至少可以看到是您的
2
和4
复制命令失败了。
关于batch-file - COPY命令-.BAT文件中的STDERR重定向导致意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58458886/