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

Delete Folder & Contents based on modified date 1

Status
Not open for further replies.

tyant

Programmer
Jun 1, 2001
68
AU
I'm not a programmer .... Technical User. I found the below script to delete files after 30 days. I hope the script will delete a folder and all its contents based on modified date

Can anyone help

Just want to delete a folder in a directory and subfolders based on modified date

It doesn't matter what date the subfolders are last modified just the main folders in the directory i'm looking at

NumberOfDaysOld = 30
strPath = "C:\Test"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)
Set colSubfolders = objFolder.Subfolders
Set colFiles = objFolder.Files

For Each objFile in colFiles
If objFile.DateLastModified < (Date() - NumberOfDaysOld) Then
objFile.Delete
End If
Next

For Each objSubfolder in colSubfolders
Set colFiles = objSubfolder.Files
For Each objFile in colFiles
If objFile.DateLastModified < (Date() - NumberOfDaysOld) Then
objFile.Delete
End If
Next

Next
 
I've also tried this but can't get it working

Any help much appreciated

NumberOfDaysOld = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
("SELECT * FROM Win32_Directory WHERE Name = 'c:\\Temp'")
For Each objFolder in colFolders
If objFolder.LastModified < (Date() - NumberOfDaysold) Then
errResults = objFolder.Delete
Wscript.Echo errResults
End If
Next

Cheers
 
If you're just trying to delete the folder then it should be something like this

Code:
NumberOfDaysOld = 30
strPath = "C:\Temp"
Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set objFolder = objFSO.GetFolder(strPath)
Set colSubfolders = objFolder.Subfolders

For Each objSubfolder in colSubfolders    
	If Date(objSubfolder.DateLastModified) < Date(Now() - NumberOfDaysOld) Then
		objSubfolder.Delete
	End If
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
That isn't working at all.

Wrong number of par in line 8
 
Sorry...should have been datevalue


Code:
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 DateValue(objSubFolder.DateLastModified) < DateValue(Now() - intDaysOld) Then
		WScript.Echo objSubFolder.DateLastModified
		'objSubFolder.Delete True
	End If
Next

use the echo's to verify the correct folders are being identified....then uncomment the delete part to actually do the deleting.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
WScript.Echo objSubFolder.DateLastModified

When I take comment away from above, I get the correct date and time

No Echo appears with the below statement

WScript.Echo objSubFolder.DateLastModified

 
It seems to work now. just changed below line

If objSubFolder.DateLastModified < DateValue(Now() - intDaysOld) Then
WScript.Echo objSubFolder.DateLastModified
objSubFolder.Delete True
End If

Thanks so much for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top