Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy files over X days old

Status
Not open for further replies.

ajtsystems

IS-IT--Management
Jan 15, 2009
80
GB
Hi

I am trying to copy files over 7 days old to an identical folder. My script so far creates a folder with a time stamp then searches through a folder structure for files older than 7 days and then copies them to the folder.

I really want to copy the file within the folder name to the destination as multiplke copies of the same file name exist in various folders at in the source folder and thi sis going to cause problems...

Here's my code so far

Set fso = CreateObject("Scripting.FileSystemObject")
StrMonth = Month(Date)
If Len(strMonth) = 1 Then
strMonth = "0" & strMonth
End If
StrDay = Day(Date)
If Len(strDay) = 1 Then
strDay = "0" & strDay
End If
StrYear = Year(Date)
strFolderName = "C:\Scripts\" & "CL_" & strMonth & "-" & strDay & "-" & StrYear & "\"
Set objFolder = FSO.CreateFolder(strFolderName)

strDir = "c:\FSO"
Set objDir = FSO.GetFolder(strDir)
getInfo(objDir)

Sub getInfo(pCurrentDir)


For Each aItem In pCurrentDir.Files
'wscript.Echo aItem.Name
If DateDiff("d", aitem.DateLastModified, Now) > 7 Then
FSO.CopyFile aitem, strfoldername
End If
Next

For Each aItem In pCurrentDir.SubFolders
'wscript.Echo aItem.Name & " passing recursively"
getInfo(aItem)
Next


End Sub




Any help would be good.

James

 
After re-reading my post it doesnt make much sense, Let me re-phrase it, LOL

Can I create a copy of a folder with a file inside it, if the file inside it is over 7 days old ?

Thanks


;o)


 
Out of curiosity, what OS are you using? If your server is 2003, why not enable ShadowCopy and be able to pull back whatever date/time you want?

Anyway, yes you can easily do what you are asking.

Code:
SourceFolder = "C:\TestFolder\"
DestFolder = "C:\Backup\"

Set fso = createobject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(SourceFolder)
  
For Each oFile In oFolder.files
	If DateDiff("d", oFile.DateCreated,Now) > 7 Then
        bWeekOld = True
    End If
Next

If bWeekOld = True Then
	oFolder.Copy(DestFolder) 
End If

Set oFile = Nothing
Set oFolder = Nothing
Set fso = Nothing

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thanks Mark,


Thats perfect for what I want, I'll integrate it into my script and test it.

The script will run on win2k and 2k3 servers so should be OK for shadow copy. if you have any info links I'd beinterested, if not I'll look on MSDN - Thanks

james
 
The script I gave you is independent of ShadowCopy. If you are not familiar with that technology you shoudl check it out. Not available on 2000 though.

Right click a drive and choose properties. Select ShadowCopies tab. Set to enable on a drive. You will then be able to view multiple copies of 'time snapshots' for the drive. It is an awesome feature.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
I have discovered a few more issues which means that the script doesnt work as I need it too.

Is there a way to copy the file over 7 days to a copy of the folder where it resides. The copy of the folder needs to contain just the files over 7 days.

I was looking into this and found the ofile.parentfolder but from what I can see it will require some instr to drop the c:\ and turn the following folder name into a vaiable.

Any help would be good
 
Not sure I follow you. Are you asking to just copy files that are older than 7 days to another location? If so then that is simple...

Code:
SourceFolder = "C:\TestFolder\"
DestFolder = "C:\Backup\"

Set fso = createobject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(SourceFolder)
  
For Each oFile In oFolder.files
    If DateDiff("d", oFile.DateCreated,Now) > 7 Then
        objFSO.CopyFile(oFile.Name,DestFolder)
    End If
Next

Set oFile = Nothing
Set oFolder = Nothing
Set fso = Nothing


I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Pretty much, but with an added twist, as I need the name of the destination folder created with the same name as the source... but destination folder must only contain files older than...

;0)
 
OK so you can grab the parent folder name, create it int he destination and copy to there.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top