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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete files older than 5 days - How?

Status
Not open for further replies.

mousie18

Technical User
Dec 12, 2000
13
0
0
IE
Hello,

I'm not a script writer but I've been asked to write a script that will delete all files in 2 users folder's that are older than 5 days.
So far, I've come up with this (from TechNet):

Const DeleteReadOnly = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("e:\users\user1\*.*"), DeleteReadOnly
objFSO.DeleteFile("e:\users\user2\*.*"), DeleteReadOnly

I've scheduled a task to run the above script & it works fine, except for the fact that it deletes all the files in the folders.

The script will to use the file creation date as the trigger for when the files will be deleted.

Can anyone explain to me how I can modify the script to delete files older than 5 days?

Thx.
AD
 
If you look a little further into the scripting.FSO API it has objects to deal with drives, folders, and files. Namely the Folder.files object which will return a collection of file objects, from which you need to obtain the DateCreated.

You would most likely do this in a loop, such as

while (there are files in the collection)
get the DateCreated of file

if datediff(DateCreated, now()) > 5
delete the file
end if
loop back
end while

I hope this helps, sorry I couldn't give more.

 
Have it sorted:

Here is the code I've used:

Dim objFSO, oFldr1, oFldr2, oFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oFldr1 = objFSO.GetFolder("E:\users\user1")
Set oFldr2 = objFSO.GetFolder("E:\users\user2")
for each oFile in oFldr1.Files
if datediff("d", oFile.DateCreated, Now ()) > 5 Then
oFile.Delete
End if
Next
for each oFile in oFldr2.Files
if datediff("d", oFile.DateCreated, Now ()) > 5 Then
oFile.Delete
End if
Next


Thanks
AD
 
you could go one step further and use a Sub, that way you wouldnt have redundant code, if thats the write word

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Call delFiles("E:\users\user1", 5)
Call delFiles("E:\users\user2", 5)

Set objFSO = Nothing

'sub to delete files in folder over intAge old
Sub delFiles(strFolderPath, intAge)
Dim oFldr, oFile

Set oFldr = objFSO.GetFolder(strFolderPath)
For Each oFile in oFldr.Files
if datediff("d", oFile.DateCreated, Now ()) > intAge Then
oFile.Delete
End if
Next

End Sub
 
sorry you should set the folder object to nothing as well

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Call delFiles("E:\users\user1", 5)
Call delFiles("E:\users\user2", 5)

Set objFSO = Nothing

'sub to delete files in folder over intAge old
Sub delFiles(strFolderPath, intAge)
Dim oFldr, oFile

Set oFldr = objFSO.GetFolder(strFolderPath)
For Each oFile in oFldr.Files
if datediff("d", oFile.DateCreated, Now ()) > intAge Then
oFile.Delete
End if
Next
Set oFldr = Nothing

End Sub
 
So this piece of code i have then. when files are 5 days or older send a email.. but doesnt work

Dim objFSO, oFldr1, oFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oFldr1 = objFSO.GetFolder("C:\temp")

for each oFile in oFldr1.Files
if datediff("d", oFile.DateCreated, Now ()) > 5 Then

Dim objOutl
Set objOutl = CreateObject("Outlook.Application")
Set objMailItem = objOutl.CreateItem(olMailItem)
'comment the next line if you do not want to see the outlook window
objMailItem.Display
strEmailAddr = "myemail@myemail.nl"
objMailItem.Recipients.Add strEmailAddr
objMailItem.Subject = "[ Auto Mail ] Data op de brander"
objMailItem.Body = "Hier moet dan komen te staan om welke data het gaat"
objMailItem.Send
Set objMailItem = nothing
Set objOutl = nothing

Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top