COPY 命令 - .BAT 文件内的 STDERR 重定向产生意外结果
COPY command - unexpected results in STDERR redirection inside a .BAT file
我正在尝试使用 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
为空。
这是因为复制命令不会将该错误消息打印到标准错误。我知道,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
现在,重复上述操作,但这次使用副本,然后作为最后一个实验,重定向 stdout (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
复制命令失败了。
我正在尝试使用 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
为空。
这是因为复制命令不会将该错误消息打印到标准错误。我知道,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
现在,重复上述操作,但这次使用副本,然后作为最后一个实验,重定向 stdout (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
复制命令失败了。