Go Premium for a chance to win a PS4. Enter to Win


Batch File to Copy Dirs to zip file with date

Posted on 2006-07-15
Medium Priority
Last Modified: 2011-10-03

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!

Question by:wta2
  • 3
  • 2
LVL 30

Expert Comment

ID: 17116610

Expert Comment

ID: 17116624
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.


Accepted Solution

SMFX earned 500 total points
ID: 17116648
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.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 17118973
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...



Expert Comment

ID: 17119603
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!

Author Comment

ID: 17127107
this works like a champ!!!!  

thanks again!!!!


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Windows 10 is here and for most admins this means frustration and challenges getting that first working Windows 10 image. As in my previous sysprep articles, I've put together a simple help guide to get you through this process. The aim is to achiev…
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

885 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