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!

script to delete files modified in a particular folder

Status
Not open for further replies.

tractng

MIS
Sep 26, 2005
21
US
Guys,

I need help with a script where I can tell it to delete files older than 2 days (date modified) in a folder called "c:\backup".

I was able to find some similar codes (see below), but this codes goes and delete folders instead files.

TIA,
Tnt
------------------------------
Option Explicit

Const intDaysOld = 30
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFolder : Set objFolder = objFSO.GetFolder("C:\temp")
Dim objSubFolder
For Each objSubFolder In objFolder.SubFolders
'WScript.Echo objSubFolder.DateLastModified
If objSubFolder.DateLastModified < DateValue(Now() - intDaysOld) Then
WScript.Echo objSubFolder.DateLastModified
objSubFolder.Delete True

End If
Next
 
Something like this:

Code:
Const intDaysOld = 2
FolderPath = "C:\temp\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.Getfolder(FolderPath)
Set colFiles = objFolder.Files
For Each objFile In colFiles
    MsgBox objFile.DateLastModified
    If objFile.DateLastModified < DateValue(Now() - intDaysOld) Then
      MsgBox objFile.DateLastModified
      objFile.Delete True
   End If
Next

Swi
 
Oops. I had the datamodified popping up twice:

Code:
Const intDaysOld = 2
FolderPath = "C:\temp\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.Getfolder(FolderPath)
Set colFiles = objFolder.Files
For Each objFile In colFiles
    If objFile.DateLastModified < DateValue(Now() - intDaysOld) Then
      MsgBox objFile.DateLastModified
      objFile.Delete True
   End If
Next

Swi
 
tractng,

Did you get this working?

Swi
 
You might want to check out the following thread:
thread1584-1254168



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