我正在尝试使用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
为空。
以下是一些快速测试供您确认。从命令行尝试以下操作:
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
个复制命令失败。