# Batch file command for comparing files using ""date specified



## Nuthan (Jul 26, 2012)

Good Day All,

Sorry for my bad English

I am trying to do backup using batch file

this is source path "E:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Backup\SBO"
this is target path "F:\July 2012"

source path contains files with yesterday, day before yesterday and 2days before yesterday date backup files (each day backup file size nearly 20GB)
But i want to copy only yesterday files to target path compaing date specified 

Please help me. I need a batch file code to do this work. 

Any help greatly appreciated

Regards,
Nuthan


----------



## Squashman (Apr 4, 2003)

Why don't you setup some backup software. Backup software will only backup a file it if has been modified or if it is a new file.

Your other alternative is to use FORFILES. It can do date based selection.


----------



## Squashman (Apr 4, 2003)

If you are just backing up 3 days of your SQL database and want to backup the backups to another location, then use ROBOCOPY with the MIRROR option.


----------



## Nuthan (Jul 26, 2012)

thanks for your reply,

No problem i can use backup software as long as my work will be done. 
but i cannot pay for the backup softwares. can you suggest me any free backup software which will do my work.

well my basic need is, create a folder itself with a computer date as a name and copy only files to that specified date.
I managed to write batch file command to create a folder with date wise, but its copying 3 days files(i.e yesterday, day before yesterday and 2 days before yesterday) to each file which makes duplication files and it needs too much memory( each day file size is 20 gb)

That's why i want a script which compares files in "source" path and copy only the files with last modified date to the "target" path
This is the batch file command i am using
@echo off
for /f "tokens=1-3 delims=:/" %%i in ("%DATE%") do set DATESTAMP=%%i.%%j.%%k 
@echo backup.%DATESTAMP% 
f:
cd \july 2012
md backup.%DATESTAMP% 
set source=E:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Backup\SBO
set target=F:\July 2012\backup.%DATESTAMP%
xcopy "%source%" "%target%" /D

Regards,


----------



## Squashman (Apr 4, 2003)

Well you are using the /D option with no date specified so of course it is going to copy everything.

Do the files in your source directory have the ARCHIVE attribute set?
If you don't know how to check that open up a CMD prompt and change to your source directory and type the ATTRIB command. Let me see the output of that.


----------



## Nuthan (Jul 26, 2012)

yes i did. it displayed all the files. Please find the attached file


----------



## Squashman (Apr 4, 2003)

Nuthan said:


> yes i did. it displayed all the files. Please find the attached file


You know you can copy and paste the text from the cmd window! 
If I have to keep opening up screen shots while I am helping you with this I am going to get a little annoyed.


----------



## Squashman (Apr 4, 2003)

Use the attrib command to remove the Archive attribute from all the files that have already been backed up.
Then with the Xcopy command you can use the /M switch to only backup files that have the Archive attribute set. When you use the /M switch with Xcopy it will then remove the archive attribute after it has copied the file.


----------



## Nuthan (Jul 26, 2012)

sorry if I use attrib command, will it delete the files from source after the files have been backed up?
I don't wanna delete any files from source path

and what is the attrib command to remove the Archive attribute from all the files?
Sorry if its stupid question
Regards,


----------



## Squashman (Apr 4, 2003)

ATTRIB does not mean DELETE.

This will remove the archive attribute from all files in a directory.

```
H:\attributes>attrib
A          H:\attributes\testfile.txt
A          H:\attributes\testfile2.txt
A          H:\attributes\testfile3.txt

H:\attributes>attrib -A

H:\attributes>attrib
           H:\attributes\testfile.txt
           H:\attributes\testfile2.txt
           H:\attributes\testfile3.txt
```
If you need to reset the archive attribute for a single file.

```
H:\attributes>attrib +A testfile.txt
```
RTM

```
H:\attributes>attrib /?
Displays or changes file attributes.

ATTRIB [+R | -R] [+A | -A ] [+S | -S] [+H | -H] [drive:][path][filename]
       [/S [/D]]

  +   Sets an attribute.
  -   Clears an attribute.
  R   Read-only file attribute.
  A   Archive file attribute.
  S   System file attribute.
  H   Hidden file attribute.
  [drive:][path][filename]
      Specifies a file or files for attrib to process.
  /S  Processes matching files in the current folder
      and all subfolders.
  /D  Processes folders as well.
```


----------



## Nuthan (Jul 26, 2012)

Good Morning,
Sorry for the late reply,
Must I do this attribute remove and adding process every day to those files which I paste to earlier message?
Is there any way to be done automatically?

Regards,
Nuthan


----------



## Nuthan (Jul 26, 2012)

Thank you . I did, and it worked. the only problem is i need to add archive attribute to 9 files manually every time


----------



## Squashman (Apr 4, 2003)

You do not remove the archive attrbute every time! That is crazy! When a file is created or modified the archive attribute is turned on. You only needed to do that once to get the cycle started correctly. You should never have to run the attrib command again. When your backups are created each day those are the only files that should have the archive attribute set. When you use xcopy it will remove the archive attribute. It is pretty straight forward. I am not sure what you are not understanding.


----------

