0

Forgive me ignorance here. I'm very new to writing batch files.

I am copying files from one mapped server drive to a different mapped server drive, and creating a log file after the copy is complete.

My script is below.

@echo off

copy /V "\\server_A\source\%date:~4,2%-%date:~7,2%-%date:~10,4%\*.txt" "\\server_B\destination\" >> "\\server_B\Logs\log_%date:~4,2%-%date:~7,2%-%date:~10,4%.txt"
if %errorlevel% NEQ 0 goto ERROR
echo Copied file successfully >> "\\server_B\Logs\log_%date:~4,2%-%date:~7,2%-%date:~10,4%.txt"
echo The files were copied to destination: \\server_B\destination\ on %Date% at %Time% >> "\\server_B\Logs\log_%date:~4,2%-%date:~7,2%-%date:~10,4%.txt"
goto EOF


:ERROR
echo Copy failed on %Date% %Time% >> "\\server_B\Logs\log_%date:~4,2%-%date:~7,2%-%date:~10,4%.txt"
echo Errorlevel %errorlevel% >> "\\server_B\Logs\log_%date:~4,2%-%date:~7,2%-%date:~10,4%.txt"
goto EOF

:EOF
pause

Currently, the log file works perfectly for success.

However, on a failure (i.e. the dated source folder does not exist), it does not send the "The system cannot find the path specified" message to the log file. Additionally, it still echo's "Errorlevel 1" even though the "system cannot find the path specified" should be errorlevel 3 (as I understand it, anyway).

Any help would be appreciated!

1 Answer 1

0

Use command >FileLogOut.txt 2>&1 to Redirect Windows cmd stdout and stderr to a single file

copy /V "\\server_A\source\%date:~4,2%-%date:~7,2%-%date:~10,4%\*.txt" "\\server_B\destination\" >> "\\server_B\Logs\log_%date:~4,2%-%date:~7,2%-%date:~10,4%.txt" 2>&1

Your if (condition) goto error can be replaced by command if return non 0 goto error using the command || goto error

@echo off

copy /V "\\server_A\source\%date:~4,2%-%date:~7,2%-%date:~10,4%\*.txt" "\\server_B\destination\" >> "\\server_B\Logs\log_%date:~4,2%-%date:~7,2%-%date:~10,4%.txt" 2>&1 || goto ERROR

echo Copied file successfully >> "\\server_B\Logs\log_%date:~4,2%-%date:~7,2%-%date:~10,4%.txt"
echo The files were copied to destination: \\server_B\destination\ on %Date% at %Time% >> "\\server_B\Logs\log_%date:~4,2%-%date:~7,2%-%date:~10,4%.txt"
goto EOF


:ERROR
echo Copy failed on %Date% %Time% >> "\\server_B\Logs\log_%date:~4,2%-%date:~7,2%-%date:~10,4%.txt"
echo Errorlevel %errorlevel% >> "\\server_B\Logs\log_%date:~4,2%-%date:~7,2%-%date:~10,4%.txt"
goto EOF

:EOF
pause

  • An alternative using variables and the operators && and ||
@echo off & setlocal

set "_dest=\\server_B\destination\"
set "_srcs=\\server_A\source\%date:~4,2%-%date:~7,2%-%date:~10,4%\*.txt"
set "_logs=\\server_B\Logs\log_%date:~4,2%-%date:~7,2%-%date:~10,4%.txt"
set "_mssg=The files were copied to destination: \\server_B\destination\ on %Date% at %Time%"

copy /v "%_srcs%" "%_dest%" >> "%_log%" 2>&1 && (
     echo\Copied file successfully & echo\%_mssg% 
    ) >>"%_log%" || >>"%_log%" ( 
     echo\Copy failed on %Date% %Time%
     echo\Errorlevel %errorlevel%
    ) 

endlocal | pause 

Additional Resources:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .