+ Post New Thread
Results 1 to 7 of 7
Scripts Thread, VBScript - Delete files over a certain age in directory and subdirectories. in Coding and Web Development; Hi, I have a pretty standard bit of VB script that I want to modify, but I don't know how ...
  1. #1
    superfletch's Avatar
    Join Date
    Nov 2007
    Location
    South
    Posts
    434
    Thank Post
    146
    Thanked 77 Times in 61 Posts
    Rep Power
    31

    VBScript - Delete files over a certain age in directory and subdirectories.

    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


    Code:
    ' 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

  2. #2
    superfletch's Avatar
    Join Date
    Nov 2007
    Location
    South
    Posts
    434
    Thank Post
    146
    Thanked 77 Times in 61 Posts
    Rep Power
    31
    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...

  3. #3

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,403
    Thank Post
    109
    Thanked 267 Times in 198 Posts
    Rep Power
    168
    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?

  4. #4
    superfletch's Avatar
    Join Date
    Nov 2007
    Location
    South
    Posts
    434
    Thank Post
    146
    Thanked 77 Times in 61 Posts
    Rep Power
    31
    Quote Originally Posted by CESIL View Post
    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.

    Quote Originally Posted by superfletch View Post
    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.

  5. #5
    hallb15's Avatar
    Join Date
    Jan 2012
    Location
    Leeds
    Posts
    405
    Thank Post
    462
    Thanked 231 Times in 149 Posts
    Rep Power
    74
    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.

  6. #6
    superfletch's Avatar
    Join Date
    Nov 2007
    Location
    South
    Posts
    434
    Thank Post
    146
    Thanked 77 Times in 61 Posts
    Rep Power
    31
    Quote Originally Posted by hallb15 View Post
    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.

    Quote Originally Posted by superfletch View Post
    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...

  7. #7

    Join Date
    Aug 2013
    Posts
    1
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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
    Last edited by GtaIon; 7th August 2013 at 05:31 PM.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 1
    Last Post: 4th October 2011, 11:07 AM
  2. Replies: 0
    Last Post: 22nd November 2010, 02:04 PM
  3. Delete files in My Music and My Pictures
    By fiza in forum Windows
    Replies: 3
    Last Post: 15th November 2010, 01:12 PM
  4. Replies: 3
    Last Post: 16th July 2010, 11:56 AM
  5. A script to delete files created by certain owners
    By Oops_my_bad in forum Scripts
    Replies: 2
    Last Post: 22nd January 2009, 11:21 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •