Question: How do I capture all the output from a Windows command like MOVE? I want the exit code, the standard output, the error message, everything.
You can capture the exit code via %ERRORLEVEL%, and the standard output via MOVE ... >file.txt or >>file.txt, but that doesn't include error messages like "Access denied".
Sometimes, technical questions involving Microsoft products are best asked of
In this case:Google: how do i [do some thing] site:microsoft.com
Google: how do i capture error messages from the move command site:microsoft.com
Happy, happy, happy...Redirecting Error Messages from Command Prompt: STDERR/STDOUT talks about the magic 1> and 2> redirection operators, and how you can use 2>&1 to redirect STDERR to STDOUT plus 1> to redirect both to a file.
Here's a move.bat command file to test the technique: it MOVEs two files to a subfolder xxx, where one of the MOVE commands is going to fail because a read-only copy of the file already exists in the subfolder:
The Windows console output is now completely incomprehensible but that doesn't matter; this technique is intended for command files that run more-or-less unattended with ECHO OFF to suppress this
ERASE "diag.txt" ECHO %DATE% %TIME% *** Testing diagnostics written from move.bat to diag.txt... >>"diag.txt" MOVE /Y "fff.txt" "xxx\fff.txt" 1>>"diag.txt" 2>&1 ECHO %DATE% %TIME% *** MOVE fff.txt ERRORLEVEL = %ERRORLEVEL% >>"diag.txt" MOVE /Y "ggg.txt" "xxx\ggg.txt" 1>>"diag.txt" 2>&1 ECHO %DATE% %TIME% *** MOVE ggg.txt ERRORLEVEL = %ERRORLEVEL% >>"diag.txt" ECHO %DATE% %TIME% *** ...all done >>"diag.txt" PAUSE
The resulting diag.txt file contains everything; Access is denied, 0 file(s) moved and ERRORLEVEL = 1:
Tue 02/25/2014 14:38:28.74 *** Testing diagnostics written from move.bat to diag.txt... Access is denied. 0 file(s) moved. Tue 02/25/2014 14:38:28.75 *** MOVE fff.txt ERRORLEVEL = 1 1 file(s) moved. Tue 02/25/2014 14:38:28.76 *** MOVE ggg.txt ERRORLEVEL = 0 Tue 02/25/2014 14:38:28.76 *** ...all done