DOS batch file to copy and generate a log file only if there's an error

I have a batch file I've written that copys a file every night over our VPN from each of our stores to the server.  The problem is that if there's an error then I don't know it.  Is there a way using a switch or DOS to generate a log.txt file only if there's an error such as if the VPN were down?  Below is an example of my file at present.

copy "\\192.168.2.121\Title Loan Professional Plus\database\titleloan2k_be.mdb" N:\Athens /y /z
flaggkortAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

t0t0Commented:
The IF condition must follow the copy command immediately like this:



   copy "\\192.168.2.121\Title Loan Professional Plus\database\titleloan2k_be.mdb" N:\Athens /y /z
   if not %errorlevel% equ 0 (
      echo %dat% %time% Error occured copying titleloan2k_be.mdb >>error.log
   )


this appends an error messege to the ERROR.LOG
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
t0t0Commented:
I meant %date% in the above code - not %dat% (a typo)

This might look neater:

copy /y /z "\\192.168.2.121\Title Loan Professional Plus\database\titleloan2k_be.mdb" N:\Athens 2>>error.log
if not %errorlevel% equ 0 (
   echo %date% %time% Error occured copying titleloan2k_be.mdb >>error.log
)

0
flaggkortAuthor Commented:
For some reason the second answer didn't work but this one worked perfectly.  Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.