No announcement yet.

Delete all Files on folder excluding certain ones

  • Filter
  • Time
  • Show
Clear All
new posts

  • Delete all Files on folder excluding certain ones

    Hiya i'm having some trouble coming up with a script.

    Basically we have a shared network drive with a GENERAL folder on it.... Everyone can see it and add to it. But no one deletes the files after they are done so now we have a disk space issue!

    I need to create a script that will delete all files and folders over a certain age (7 days for example.) but will allow me to exclude certain folders.

    This is a pain i know. I been looking all over and trying to do certain ideas and struggling.

    I have been looking at the posts on these forums and trying to modify them to help but with no luck.

    I hope some one can help me out with this.

    Many Thanks

  • #2
    Re: Delete all Files on folder excluding certain ones

    EDIT - actually the post that you have listed has a much better should be fairly easy to modify, its just the number of days and folder location that your going to change. Please post what you have so far and we will help you. Thanks.

    Maybe something like this?!?!? Have not tested...

    ' delete all files in a folder that are 7 days old
    strFolder = "C:\test\"   'folder to run the script against
    dtmYesterday = Date - 7  'the "7" here represents the number of days old
    strYear = Year(dtmYesterday)
    strMonth = Month(dtmYesterday)
       If Len(strMonth) = 1 Then
          strMonth = "0" & strMonth
       End If
    strDay = Day(dtmYesterday)
       If Len(strDay) = 1 Then
          strDay = "0" & strDay
       End If
    strYesterday = strYear & "-" & strMonth & "-" & strDay
    strFileName = strFolder & "*." & strYesterday &"-*"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
       If NOT InStr(strFileName, "FileToExclude") _
           OR InStr(strFileName, "Another_FileToExclude") then
       End If
    msgbox "Done!"
    That's really all I can think of for vbscript...but I would suggest using a tool like Robocopy to delete files and folders over a certain age.
    Last edited by ekrengel; 21st October 2009, 15:30.