# Batch file backup by mod date



## unolax (Feb 16, 2007)

Hey guys I have made a few quick autorun .bat files to do quick backups within my office. My next step to to just pull the mod date and check that against the date of my last backup and copy only those files. Right now I am just overwriting all files in folders where my spreadsheets change. Current Autorun.inf is 


```
[autorun]
icon=OC.ico
Label=OC5
shellexecute=Backup.bat
action = Backup.bat
useautoplay=1
```
And my .bat file is as follows 


```
xcopy "C:\Users\admin\Desktop\folder" "\folder" /d /e /f /h /r /y /c
xcopy "C:\Users\admin\Desktop\Name" "\Name" /d /e /f /h /r /y /c 
xcopy "C:\Users\admin\Desktop\Saved Job Sheets" "\Saved Job Sheets" /d /e /f /h /r /y /c 
xcopy "C:\Users\admin\Desktop\Specialty Parts Tracking" "\Specialty Parts Tracking" /e /f /h /r /y /c
xcopy "C:\Users\admin\Desktop\Done" "\Photos\Done" /d /e /f /h /r /y /c 
echo. |time |find "current" >> \Backuplog.txt
echo. |date |find "current" >> \Backuplog.txt
echo. |time |find "current" >> C:\Backup\Backuplog.txt
echo. |date |find "current" >> C:\Backup\Backuplog.txt
```
As I said my final goal with this is to be able to just plug these in get a quick updated backup including any files that have been modified since the last backup. Any help would be greatly appreciated as I have been banging my head against this for almost 2 weeks now. 
Thanks again,
UnoLax


----------



## TheOutcaste (Mar 19, 2009)

You are already doing that for every folder but this one:
"C:\Users\admin\Desktop\Specialty Parts Tracking".

You are using the /D switch so Xcopy will only copy files that have a more recent Modified date than the date on the files in the backup.


```
/D:m-d-y     Copies files changed on or after the specified date.
             [B]If no date is given, copies only those files whose
             source time is newer than the destination time[/B].
```
Add the /D switch to the one that is missing it and that should take care of it.

You can put all the switches to getther as well:
*Xcopy /cdefhry "Source" "Dest"*
You can also assign them to a variable, this makes it eady to add one, like /L to list the files that will be copied without actually copying them:
*Set _Args=/cdefhry
Xcopy %_Args% "Source" "Dest"*


----------



## unolax (Feb 16, 2007)

The one that is missing the /D is so that it overrides all of the files, as there are budget sheets in there that change daily. With the /D in there it would only add the new files, not overwrite the newly modified ones. I am not sure though, I may be using these switches wrong.


----------



## TheOutcaste (Mar 19, 2009)

*/D* will copy new files, and copy files that have been modified. Files that have not changed will not be copied.
Without the */D*, every file is copied again. even if it hasn't been modified.

Easy to test. Add the */L* switch. Run the program see what files will be copied with and without the */D* switch. Add a new file and modify an existing file, then check again what files will be copied with and without the */D* switch.


----------



## unolax (Feb 16, 2007)

thanks! that worked!


----------



## unolax (Feb 16, 2007)

This is the final that I got working. It pulls the current date and then places it in the /D to pull anything that was modified today. Thank you all for all your help!

```
echo on
SET date=%date:~4,2%-%date:~7,2%-%date:~10,4%
echo %date% 
xcopy "C:\Users\admin\Desktop\folder" "\folder" /d:%date% /e /f /h /r /y /c
xcopy "C:\Users\admin\Desktop\Name" "\Name" /d:%date% /e /f /h /r /y /c 
xcopy "C:\Users\admin\Desktop\Saved Job Sheets" "\Saved Job Sheets" /d :%date%/e /f /h /r /y /c 
xcopy "C:\Users\admin\Desktop\Specialty Parts Tracking" "\Specialty Parts Tracking" /e /d:%date% /f /h /r /y /c
xcopy "C:\Users\admin\Desktop\Done" "\Photos\Done" /d:%date% /e /f /h /r /y /c 
echo. |time |find "current" >> \Backuplog.txt
echo. |date |find "current" >> \Backuplog.txt
echo. |time |find "current" >> C:\Backup\Backuplog.txt
echo. |date |find "current" >> C:\Backup\Backuplog.txt
```


----------



## TheOutcaste (Mar 19, 2009)

One problem with that is it will not backup anything modified or added yesterday _after_ the backup is run.
Picture this:

Run backup on Monday
Someone modifies a file right after backup is run.
A new file is added.
Run backup on Tuesday.
The file that was modified on Monday will NOT be backed up.
The new file will NOT be backed up.
You forget to run backup on Wednesday.
Files are modified and added.
Run backup on Thursday.
All files modified on Wednesday will NOT be backed up.
All new files added on Wednesday will NOT be backed up

You want to use just /D with no date specified. This will copy all files modified OR added since the backup was last run.

Also, you shouldn't assign values to the special system variables like date. No other program that runs in this command session will be able to use it after that unless you first close the Command Prompt window.

If you run this program twice without closing the Command Prompt window it will not give you a valid date.

1st run, date=*11-09-2010*
2nd run, date=*9--01-*
3rd run, date=*1---*

Use a different name like *_Date*, or *ModDate*


----------



## unolax (Feb 16, 2007)

okay thank you


----------

