Batch File to Copy Dirs to zip file with date

Exactly what I am trying to do:

1. I have 2 directories that need to be backed up.
2. I need to get these folders, subfolders, and files to a server for nightly backup.

What I have done already:

1. I have already manually copied the folder/files to he server.
2. I have written a batch file (very basic), using xcopy, that copies only the files that have updated in 2 directories.

What I would "like" to do:

1. I would like to add to the batch file a way to write the files that were copied to a log.
2. I an also going to add this batch file to scheduler to be run every night at 10pm.
2. If there is a more "correct" way, please suggest!

Some things that have crossed my mind while writing this:

1. Maybe zipping up these directories to a file with the "date" in the file name? But what about logging?
2. Maybe creating new directories on the server with the "end of day date" for the directory?

If anyone has suggestions, a better way, or whatever....any input would be greatly appreciated!

Who is Participating?

Improve company productivity with a Business Account.Sign Up

SMFXConnect With a Mentor Commented:
Also, if you want to include dates, you can use the system %DATE% variable to accomplish this in a basic fasion.  However, you have account for the special characters in the result.  For instance, on a default install if you type "echo %DATE%" at the command prompt, you'd get something like:
    Sat 07/15/2006

The slashes (/) are invalid and you may not want to bother with the "Sat", so you can just use the variable expansion settings of the command line to strip out only the pieces you want.  The format is:
Where S is the start character and N is the number of characters to include.  So, the month would be %DATE:~4,2%, the day would be %DATE:~7,2%, and the year would be %DATE:~10,4%.  Thus typing "echo %DATE:~4,2%%DATE:~7,2%%DATE:~10,4%":
All nice and clean.  Now when you're displaying files, folders, or anything else you may want to sort by the string value, you would probably want to do Reverse Polish Notation or YYYYMMDD.

Okay, add that all to our robocopy command and here you get a nifty little result:
    robocopy.exe "\\server\source" "\\machine\destination\backup_%DATE:~10,4%%DATE:~7,2%%DATE:~4,2%" /E /B /COPYALL /M /LOG+:"X:\logfiles\backup_%DATE:~10,4%%DATE:~7,2%%DATE:~4,2%.log" /TEE

Notice I put the file paths in quotes as some of the paths could wind up with spaces (including the date depending on your system format).  This ensurse robocopy doesn't treat the space as the beginning of the next parameter and screw up the script.

The result is a new directory in your destination titled "backup_YYYYMMDD" that the changed files are copied to.  Also, a seperate corresponding log file for each day, "backup_YYYYMMDD.log".

Finally, if you just want to put them all into one big file, I'd recommend you just use the built-in NTBackup and backup to a file.   Use an incremental backup to only backup the changes.  You can find good documentation in the online doc's of the program (ntbackup.exe).  This will do the scheduling, reporting, logging, and dating for all of your data.

Rather than using xcopy, you may want to look at Robocopy that is part of the Windows Resource Kit as a download from Microsoft:

Look at the .DOC that comes with it, but you'd be looking at something like:
    robocopy.exe \\server\source \\machine\destination /E /B /COPYALL /M /LOG+:X:\logfiles\backup.log /TEE

You could also create a job file from this for future use if you'd like using the /SAVE option.

Basically, what this does is copy all files from the source (\\server\source) to the destination (\\machine\destination) in all subdirectories (/E) using a backup method (/B) and include all security, attribute, & ownership attributes of any files and folders (/COPYALL), of only those files that have changed since the last copy (/M), save it to a log file (/LOG+:X:\logfiles\backup.log) and display the action as its happenning (/TEE).

Robocopy is a much more feature rich file copy than xcopy, so is more useful for this items.

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

wta2Author Commented:
Thank you VERY MUCH....this works great...

This may be a stupid question, but how/where does robocopy compare or know what files have been updated? Since the files are not being copied into the same directory every time to compare dates? or ?

Just a little confused.....

Again, Thank you very much...


The /M (Modified) parameter looks at the Archive attribute of the file(s).  This is like the system, hidden, and read-only attributes of files.  The Archive attribute is special in that any time a program saves, creates, or changes a file (ie: Modified), it sets the Archive attribute to true.  The /M attribute causes robocopy to copy only those files that have the archive attribute set to true and then changes the flag to false indicating it has been backed up.  Thus next time it runs, it will only copy those files that have been editted and had the Archive attribute set back to true.

There's a lot of good features in robocopy that should be included in the .DOC file; I recommend you take a look at it for more details.  At the very least, just run robocopy.exe /? from a command line or a brief rundown of what's available.

Glad this could help!
wta2Author Commented:
this works like a champ!!!!  

thanks again!!!!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.