我正在尝试使用以下[.BAT文件中的COPY命令和重定向STDERR句柄记录每个文件传输:
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
我原本希望在CopyError.log中看到2条错误行,以显示“系统找不到指定的路径。”但CopyError.log为空。
将提供任何帮助。
这是因为复制命令不会将该错误消息打印到stderr。我知道,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.
不幸的是,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
个复制命令失败。