Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch File to Copy Dirs to zip file with date

Posted on 2006-07-15
6
Medium Priority
?
447 Views
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!

TIA,
Bill
0
Comment
Question by:wta2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 30

Expert Comment

by:callrs
ID: 17116610
0
 
LVL 3

Expert Comment

by:SMFX
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:
    http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&DisplayLang=en

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.

HTH,
-SMFX
0
 
LVL 3

Accepted Solution

by:
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:
   %DATE:~S,N%
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%":
   07162006
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.

HTH,
-SMFX
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:wta2
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...

Bill

0
 
LVL 3

Expert Comment

by:SMFX
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!
-SMFX
0
 

Author Comment

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

thanks again!!!!

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hello I read in a discussion about a person who configured a very simple mirror RAID with two hard drives; the system and data were on the same partition. He asked how to repair the system as it was not booting up anymore. In his case running …
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
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.

661 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