superfletch Posted February 7, 2013 Posted February 7, 2013 Hi, I have a pretty standard bit of VB script that I want to modify, but I don't know how to go about making the change I require. The script deletes all files within a specified directory over a certain age, given in days. (In the example its: C:\MyTestDir and 2 days). It's fine and works really well. What I'd like it to do now is delete all files over the specified age in all subfolders of the directory that is given as well as the specified directory itself. I've tried with adding a trailing \ and with adding \* but these don't work. Any VB Genius out there who can help me? TIA Matt ' This file will find and delete every file in the specified directory'' that is older that the specified amount of days. ' '******************************************************************' ' Change the vairables below to change the destination directory and ' the amount of days old the file must be to get deleted. Dim strFolder strFolder = "C:\MyTestDir" Dim intDays intdays = 2 '' DO NOT EDIT THIS FILE BELOW THIS LINE ''' Dim objFileSystem, objDirectory, objFiles, objFile, objTextStream set objFileSystem = CreateObject("Scripting.FileSystemObject") set objTextStream = objFileSystem.OpenTextFile("dellog.log",8,True) objTextStream.WriteBlankLines 2 objTextStream.WriteLine "***************" & vbcrlf & "Program started " & Time() & " on " & date() & vbcrlf set objDirectory = objFileSystem.GetFolder(strFolder) Set objFiles = objDirectory.Files objTextStream.WriteLine "The following files were deleted." & vbcrlf For Each objFile in objFiles If objFile.DateLastModified < (Date() - intDays) then objTextStream.WriteLine objFile.name & vbtab & vbtab & vbtab & "created on: " & objFile.DateCreated objFile.Delete true End if Next objTextStream.WriteLine vbcrlf & "Program ended " & Time() & " on " & date() & vbcrlf & "***************" objTextStream.close
superfletch Posted February 8, 2013 Author Posted February 8, 2013 For anyone who looked at this - I used something different in the end (forfiles.exe) from the Win Resource Kit. If anyone does know how to do this in VB - I'd still be interested...
CESIL Posted February 8, 2013 Posted February 8, 2013 That script works ok here...I just had turned off the delete bit as I still wanted the files that were found... What part is not working for you?
superfletch Posted February 8, 2013 Author Posted February 8, 2013 That script works ok here...I just had turned off the delete bit as I still wanted the files that were found... What part is not working for you? I wanted it to work through Subdirectories as well as the one set. It's fine and works really well. What I'd like it to do now is delete all files over the specified age in all subfolders of the directory that is given as well as the specified directory itself. I've tried with adding a trailing \ and with adding \* but these don't work.
hallb15 Posted February 8, 2013 Posted February 8, 2013 Does it have to be VB script? I use robocopy to do a similar thing, except I move the files to another location called archive. Once they haven't been missed for 6 months, they get deleted. Use the /minage: switch to specify how old the document to be delete has to be, then use the /r:0 so it doesn't stop if it finds a locked file.
superfletch Posted February 11, 2013 Author Posted February 11, 2013 Does it have to be VB script? No - As stated - I've now done it differently, but I'd just be interested... ...for the future. For anyone who looked at this - I used something different in the end (forfiles.exe) from the Win Resource Kit. If anyone does know how to do this in VB - I'd still be interested...
GtaIon Posted August 7, 2013 Posted August 7, 2013 (edited) IF it were me, I would probably just make a Sub procedure to do a recursive look through all of the sub directories. I haven't tested this, but it would probably just call a sub after line 38 or the word "next". It would probably look something like the following. CleanSubFolders objFileSystem.GetFolder(strFolder) Sub CleanSubFolders(Folder) For Each Subfolder in Folder.SubFolders Set objFolder = objFileSystem.GetFolder(Subfolder.Path) Set colFiles = objFolder.Files For Each objFile in colFiles If objFile.DateLastModified < (Date() - intDays) then objTextStream.WriteLine objFile.name & vbtab & vbtab & vbtab & "created on: " & objFile.DateCreated objFile.Delete true End if Next CleanSubfolders Subfolder Next End Sub Edited August 7, 2013 by GtaIon
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now