Link to home
Start Free TrialLog in
Avatar of Kerry Hill
Kerry HillFlag for United States of America

asked on

Need a script to loop through folders to look for a folder called "common" and delete all files older than 45 days with the exception of certain extensions and log the activity in a log file.

I am looking for a script that will look in a folder that is like a "users" folder.  It needs to find all the directories named "common" under these user folders and delete all the files in this directory and all the subdirectories older than 45 days.  We need it to create a log file for each day this ran with all the files deleted listed.
Avatar of boowhup
boowhup
Flag of New Zealand image

This script searches a folder (strBaseFolder) and all subfolders for "common" in the name (you can define this in strSearchFile). It then tries to delete very file in that folder (but not subfolders) and if successfully deleted writes to a logfile.
strSearchFile = "common"
strBaseFolder = "d:\"
strdate = year(now) & month(now) & day(now)
strLogFile = ".\log-" & strdate & ".csv"
intOldDate = 45
'========================
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objBaseFolder = objFSO.GetFolder(strBaseFolder)
WriteToTxt "Time,Filename(Short),Filename(Full)"
ProcessFolder(objBaseFolder)
'=========================
 
Sub ProcessFolder(objFolder)
	On Error Resume Next
	For Each objSubFolder in objFolder.SubFolders
		For Each strExtension in arrDirectoryExtensions 
			if Instr(objSubFolder.Name,strSearchFile) then
				DeleteFiles(objSubFolder)
			end if
		Next
		ProcessFolder(objSubFolder)
	Next
	
	On Error Goto 0
End Sub
 
Function DeleteFiles(objFolder)
	For	Each objFile in objFolder.Files
		On Error Resume Next
		objFile.Delete
		if Err.Number = 0 then
			WriteToTxt GetDetails(objFile)
		end if
		On error Goto 0
	Next
End Function

Open in new window

Avatar of Kerry Hill

ASKER

Thanks, will this continue to look for subfolders named Common after it finds the first one?  There will be multiple "common" folders.
yes, it will go through every sub folder from the "base" (root) folder , and work against each "common" found , then keep going. One thing I should mention, it looks for the word "common" in the name, so it would work against "xxcommonxx" as well as "common". I can change if necessary.
Great, is there a way to make is delete files over 45 days in the common folder recursively?
Avatar of Robin CM
you'd need to define what you mean by "over 45 days". 45 days since when? Creation date? Last Accessed date? Last Modified date?

Just need to tweak the delete function as follows.

Swap objFile.DateLastModified for objFile.DateCreated or objFile.DateLastAccessed as appropriate.

The script above uses a sub called WriteToTxt to write the log but the code for that isn't supplied. You can just knock something basic up using objFile.WriteLine (as shown). Just make sure you set sLogFile at the top of the script.


Function DeleteFiles(objFolder)
	For	Each objFile in objFolder.Files
		On Error Resume Next
		If (Now() - objFile.DateLastModified) > intOldDate Then
			objFile.Delete
		End If
		if Err.Number = 0 then
			WriteToTxt GetDetails(objFile)
		end if
		On error Goto 0
	Next
End Function
 
Dim sLogFile
sLogFile = "C:\scripts\basiclog.txt"
 
Sub WriteToTxt(sText)
 
	Dim oFSO, oFile
	Set oFSO = CreateObject("Scripting.FileSystemObject")
	Set oFile = oFSO.OpenTextFile(sLogFile,8,True)
	oFile.WriteLine Now()&" "&sText
 
End Sub	

Open in new window

I apologize that it is taking a little while to respond, I am a little slow when it comes to asp script.  Thank you very much for the responses, I should be able to respond tomorrow about this.
well spotted robincm, I think I pasted in a WIP.  it should have been this...

And robin's comment re changing from datelast modified / date last accessed is something to think about. Although If I remember correctly datelastmodifed is always later than datecreated and dateaccessed will be updated to now() as soon as it's touched by the filesystemobject??

I would leave it as datelastmodified, as that is the one that shows in windows explorer, and the one in a dos dir command - so would be the one you would manually compare against when testing the script.


strSearchFile = "common"
strBaseFolder = "d:\"
strdate = year(now) & month(now) & day(now)
strLogFile = ".\log-" & strdate & ".csv"
intOldDate = 45
'========================
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objBaseFolder = objFSO.GetFolder(strBaseFolder)
WriteToTxt "Time,Filename(Short),Filename(Full)"
ProcessFolder(objBaseFolder)
'=========================
 
Sub ProcessFolder(objFolder)
	On Error Resume Next
	For Each objSubFolder in objFolder.SubFolders
		For Each strExtension in arrDirectoryExtensions 
			if Instr(objSubFolder.Name,strSearchFile) then
				DeleteFiles(objSubFolder)
			end if
		Next
		ProcessFolder(objSubFolder)
	Next
	
	On Error Goto 0
End Sub
 
Function DeleteFiles(objFolder)
	For	Each objFile in objFolder.Files
		On Error Resume Next
		If (Now() - objFile.DateLastModified) > intOldDate Then
			objFile.Delete
		End If
		if Err.Number = 0 then
			WriteToTxt GetDetails(objFile)
		end if
		On error Goto 0
	Next
End Function
 
Function GetDetails(objF)
	GetDetails = objF.Path & "," & GetDetails 
	GetDetails = objF.Name & "," & GetDetails 	
End Function
 
Sub WriteToTxt(strWrite)
	'Appends lines to Log File
	Set FSO = CreateObject("Scripting.FileSystemObject")
	Set objOutputFile = FSO.OpenTextFile(strLogFile, 8, True)
	objOutputFile.WriteLine(Now & "," & strWrite)
	objOutputFile.close
End Sub

Open in new window

Ok, this is looking very nice, thank you, I may actually set it to createddate in the end because people will go in and modify stuff just to avoid it being deleted -- yeah, they would actually do that.  The other thing that happens is they bury the files in folders, is there a way for it to perform the delete recursively from the "common" folder?
Unfortunately, my scripting skills are truly non-existant, something I need to pick up, but is it already doing the recursive bit?  Is that this bit:  For Each objSubFolder in objFolder.SubFolders?
At the moment it will only delete the folders in the "common" folder, not any files in subfolders, but it does check all subfolders for another common folder (thats the recursive part). If you want it to delete every file under the common folder, includng all files found in the subfolders (and their subfolders etc ) I can change it to do that. Do you want it to leave the empty subfolders behind, or delete those too?

Yes, I want it to leave the empty folders behind, but any folders under the common folder, it should delete all the files older than 45 days.
Boowhup,

I have tested this, and it works but I need it to delete the files recursively under each "common" folder it finds.  Is this possible?

ASKER CERTIFIED SOLUTION
Avatar of boowhup
boowhup
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much, I hope you are feeling ok.  Don't feel bad at all about the time, you are helping me.  I just get the emails about needing to stay in touch, so I try to update my questions every few days.  I will test this tomorrow and get back to you.  Thanks again for all of your help, you have no idea how helpful this will be.
This works great!  Thank you very much!
Thank you very much this works!