# Batch File to Copy files based on date



## kayda

Hello:

I want to move files from one folder to another based on the creation date of the file. Specifically every day I want to move files that are more than 30 days old.

How to do?

Kayda


----------



## Squashman

You can use Forfiles to do this.
http://technet2.microsoft.com/windo...65c7-48cf-b466-204ba159381e1033.mspx?mfr=true
You can get it from here.
http://www.dynawell.com/download/reskit/microsoft/win2000/forfiles.zip

I belive you can do it with Robocopy as well. Which I believe is in the XP Resource Tools
http://www.ss64.com/nt/robocopy.html
http://www.petri.co.il/download_windows_xp_reskit_tools.htm


----------



## ghostdog74

there's no need to download any stuffs. Use native vbscript


Code:


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\temp")
For Each efile in objFolder.Files
 'Wscript.Echo "file is " & efile
 'WScript.Echo eFile.DateCreated
 If DateDiff("d",eFile.DateCreated,Now) >= 30 Then
   WScript.Echo "file found that is 1 month old: " & efile
   objFSO.DeleteFile(eFile)
 End If 
Next


----------



## Squashman

ghostdog74 said:


> there's no need to download any stuffs. Use native vbscript
> 
> 
> Code:
> 
> 
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFolder = objFSO.GetFolder("C:\temp")
> For Each efile in objFolder.Files
> 'Wscript.Echo "file is " & efile
> 'WScript.Echo eFile.DateCreated
> If DateDiff("d",eFile.DateCreated,Now) >= 30 Then
> WScript.Echo "file found that is 1 month old: " & efile
> objFSO.DeleteFile(eFile)
> End If
> Next


Forfiles and Robocopy are MS Utiltiies and would take one line of code VS writing out this whole VBscript. If this is just a one shot deal then yeah, the VBscript is great. But if you are writing a batch file that is piping the data then I would suggest using forfiles or robocopy.


----------



## ghostdog74

It really doesn't matter one line of code or not. Internally, forfiles/robocopy does the same thing as what the script is doing by using various switches and options. I could jollywell call the vbscript robocopy.vbs. Then next time i want to run it in a batch , i just call it using cscript. There you go, one line of code. 
Furthermore, if i have many machines to administer, i won't have to download and install in every machine. wsh, on the other hand, is installed already.

Anyway, its really up to OP.


----------

