batch-filecmderror-handlingcopystderr

COPY command - unexpected results in STDERR redirection inside a .BAT file


I am trying to log each file transfer using COPY command and the redirect STDERR handle in the following .BAT file:

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

I was expecting to see 2 error lines in the CopyError.log to show "The system cannot find the path specified." but instead CopyError.log is empty.


Solution

  • Unfortunately, Copy doesn't output that message as StdErr.

    Previously XCopy was offered as an alternative. See this question for more information, however here's a quick idea for you:

    (   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"
    

    Now, as with your intended method, this will not tell you which of the commands actually output the message. If you wanted to do that, you could, I suppose, output the line number:

    (   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"
    

    Here, the error should be prepended with the number, in this case:

    2:The system cannot find the file specified.
    4:The system cannot find the file specified.
    

    You will at least then be able to see that it was your 2nd and 4th copy commands which failed.