[Webinar] Streamline your web hosting managementRegister Today


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

Posted on 2010-04-07
Medium Priority
Last Modified: 2012-05-09
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 "\\\Title Loan Professional Plus\database\titleloan2k_be.mdb" N:\Athens /y /z
Question by:flaggkort
  • 2
LVL 16

Accepted Solution

t0t0 earned 500 total points
ID: 30032785
The IF condition must follow the copy command immediately like this:

   copy "\\\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
LVL 16

Expert Comment

ID: 30033102
I meant %date% in the above code - not %dat% (a typo)

This might look neater:

copy /y /z "\\\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


Author Closing Comment

ID: 31711898
For some reason the second answer didn't work but this one worked perfectly.  Thanks!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…
Suggested Courses

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question