# Using DOS batch files to perform a backup



## GatinGamer (Nov 18, 2007)

So I have a rather puzzling situation. I've decided that paying for backup software when all I really want out of this backup is to copy the folders exactly makes no sense, and doing that manually is a bad option because I want an automated backup. I have written a batch file that will copy the folders using *xcopy*, thus performing a full backup of the needed data. I want to make a second batch file that will perform a synthetic full backup, in case you are unfamiliar with the term it's basically using *xcopy* with the /D option where the date is the last date a backup happened. I think the best way to do this is to use the DOS *date* function as input to a text file and then read the last run date from a text file when i start the backup. Only trouble with doing it this way is that the *date* function returns the date with a day of the week and *xcopy* requires the date without. So far I have attempted to write the output to a file then read from a different file but am having trouble figuring out how to parse it properly. My current method is involving C and is going to require a second script. Is there some way that I can do this using just one batch and one text file?


----------



## ChuckE (Aug 30, 2004)

If you use the *XCOPY /D* with no date given, then it copies only those files whose source time is newer than the destination time. So then, only files that have been updated will be copied.

What's wrong with that? Won't it do what you want?


----------



## GatinGamer (Nov 18, 2007)

Well see the tricky part is I don't just want to copy the files that have changed, I want to copy the files that have changed since a given date. I ended up writing a C function to do the date parsing I needed and compiling it as an executable and calling that from within the batch script.


----------



## ChuckE (Aug 30, 2004)

You might want to look over this guy's solution: AUTOMATIC COPYING BY DATE.


----------



## GatinGamer (Nov 18, 2007)

That link, though useful, only does a portion of what I was looking for. It will copy by default anything updated today or before the date i type in. If I run that everyday it mimics the functionality I have coded for, I was planning on running it on Monday, Wednesday, and Friday so I need it to store those dates somewhere. I just wrote a small C program to do that for me and did the rest with the batch file.


----------



## ChuckE (Aug 30, 2004)

There is still a way by using the DOS *AT /every:date,M,W,F yourbatch.bat* command,
(or whatever you might want in the "yourbatch.bat" position).


----------



## TheOutcaste (Aug 8, 2007)

The set command allows you to extract a substring from a variable:
for example:

```
date /t >c:\lastbackup.txt          ;lastbackup.txt now has Mon 11/26/2007
set /p mydate= <c:\lastbackup.txt   ;mydate now equals Mon 11/26/2007
set mydate=%mydate:~4%              ;mydate now equals 11/26/2007
```
 you can now use %mydate% in the xcopy command

see help for the set command
HTH

Jerry


----------

