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!

Search and delete files in subfolders

Status
Not open for further replies.

maltman1

Technical User
Nov 27, 2007
15
0
0
US
Hey all,

First off, I am not a programmer but I need to create a script to delete files on one of our servers. I have a working script that will delete all files in a specific folder and all of it's subfolders after 30 days. I need to add some sort of a wildcard or exclusion for a few specific folders to delete the files after 90 days. I know normal wildcards don't work with the File System Object. Is there anything I can do to get this working? Thank you.

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
 
Hey maltman1,

Im not programmer either, but i had an idea that may lead you to the answer - and yes as far as I know there is no way to pass a wildcard through fso.

The key to this work around would be to find a common unique quality that these folders share - if you can find that, then it should be easy to exclude them with another if statement.

Are there any unique properties that will be constant with these folders? name? attributes?

If you have the luxury that these folders are generated from another program you designed, then I would suggest setting a common property and going from there.

seg284
 
Thank you for responding seg284. There is a common property with these folders. They all begin with the word Fax in the folder name. I was trying the wildcard "Fax*" but of course this did not work. I have tried adding a Case statement to the script but it did not work the way I wanted it to. But, it could have always been user error. My problem now is getting the correct syntax for another If statement. Let me know if you have any ideas.

Thanks,

Matt
 
Test the following out to see if it works for you:

'****
For Each objSubfolder in colSubfolders

If Left(objsubfolder.Name, 3) = "fax" Then
MsgBox objsubfolder.Name
End If

Next
'****

let me know if you need anything else

Sg284
 
I have added the code to the script but I do not think it is looking at the folder. It basically does the same thing it did before the extra code was added in. But of course I could have done something wrong. Here is the code:

NumberOfDaysOld = 30
NumberOfDaysOldTemp = 90
strPath = "C:\Documents and Settings\maltman\Desktop\New Folder"

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 Left(objsubfolder.Name, 4) = "temp" AND objFile.DateLastModified < (Date() - NumberOfDaysOldTemp) Then
objFile.Delete
Else If objFile.DateLastModified < (Date() - NumberOfDaysOld) Then
objFile.Delete
End If

End If

Next

Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top