+ Post New Thread
Results 1 to 7 of 7
Scripts Thread, Script to delete all folders older then 2 days. in Coding and Web Development; I currently have a script that I run a task for weekly to delete log files. I need to modify ...
  1. #1

    Join Date
    Sep 2008
    Location
    Virginia Beach
    Posts
    1
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Script to delete all folders older then 2 days.

    I currently have a script that I run a task for weekly to delete log files. I need to modify it to delete all folders and their contents within a certain directory that are over 2 days old. I cant quite figure it out. Any help would be appreciated. This is what I use for the file deletion:

    ' SET THE DROPDATE VARIABLE TO TODAYS DATE MINUS 30 DAYS
    dropdate = dateadd("d", -30, date())

    ' CREATE A FILESYSTEM OBJECT
    set fso = createobject("scripting.filesystemobject")

    ' CREATE A FOLDER OBJECT (USING THE FILESYSTEM OBJECT)
    set folders = fso.getfolder("C:\WINDOWS\system32\LogFiles\W3SVC1 ")

    ' CREATE A FILES OBJECT (USING THE FOLDER OBJECT THAT'S USING THE FILESYSTEM OBJECT.)
    set files = folders.files

    ' LOOP THROUGH EACH FILENAME IN THE FILES OBJECT
    for each file in files

    ' COMPARE THE FILES 'DATE LAST MODIFIED' PROPERTY
    ' IF THE FILES 'DATE LAST MODIFIED' IS MORE THAN DROPDATE, THEN ECHO SOME OUTPUT AND DELETE THE FILE
    if file.datelastmodified < dropdate then

    'wscript.echo folders.path & "\" & file.name & " last modified: " & file.datelastmodified
    file.delete

    on error resume next

    end if

    next

  2. #2


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339
    one liner?
    Code:
    find ./ -type d -mtime +2 -exec rm -rf {} \;

  3. #3


    Join Date
    Dec 2005
    Location
    In the server room, with the lead pipe.
    Posts
    4,681
    Thank Post
    279
    Thanked 783 Times in 610 Posts
    Rep Power
    224
    Quote Originally Posted by CyberNerd View Post
    one liner?
    Code:
    find ./ -type d -mtime +2 -exec rm -rf {} \;
    Which would actually work if you install the unix utils for windows.

  4. #4

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,240
    Thank Post
    1,058
    Thanked 1,069 Times in 625 Posts
    Rep Power
    740
    Why not use Xcopy or Dir with the /d option [ date ] to copy / List the relevant files first, then delete them afterwards.....? [ whilst Xcopying / Listing them, pipe the contents to a .txt file, tidy up the .txt file, then use that as a template / answer file to insert the delete command......

    [ Bit of a long way round using Batch - I was personally use Autoit with the FileGetTime function..... ]

  5. #5
    Marci's Avatar
    Join Date
    Jun 2008
    Location
    Wakefield, West Yorkshire
    Posts
    896
    Thank Post
    84
    Thanked 235 Times in 194 Posts
    Rep Power
    82
    Open Notepad, copy and paste, save, rename to 48hrdelete.vbs
    Amend the path {Win32_Directory.Name='C:\WINNT\system32\spool\PRI NTERS'} as necessary.

    Code:
    strComputer = "."
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    
    Set colFiles = objWMIService.ExecQuery _
        ("ASSOCIATORS OF {Win32_Directory.Name='C:\WINNT\system32\spool\PRINTERS'} Where " _
            & "ResultClass = CIM_DataFile")
    
    strCurrentDate = Now
    
    For Each objFile In colFiles
        strFileDate = WMIDateStringToDate(objFile.CreationDate)
        intHours = DateDiff("h", strFileDate, strCurrentDate)
        If intHours >= 48 Then
            Wscript.Echo objFile.Delete
        End If
    Next
    
    Function WMIDateStringToDate(dtmInstallDate)
        WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _
            Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) _
                & " " & Mid (dtmInstallDate, 9, 2) & ":" & _
                    Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, _
                        13, 2))
    End Function
    S'what I use to empty printjobs out that are more than 2 days old that have gotten stuck somewhere in the queue...

    Call it using a batch file so you can stop and restart services...

    Code:
    @echo off
    echo Stopping TCP/IP Print Server
    net stop "TCP/IP Print Server"
    echo Stopping Print Spooler
    net stop "Print Spooler"
    echo
    echo Deleting all spool files older than 48 hours
    cscript 48hrdelete.vbs
    echo
    echo Restarting Print Spooler
    net start "Print Spooler"
    echo Restarting TCP/IP Print Server
    net start "TCP/IP Print Server"
    echo Complete
    God knows where the vbs came from - took some hunting on Google at the time... butcher as required if useful.

  6. Thanks to Marci from:

    contink (15th November 2010)

  7. #6
    altecsole's Avatar
    Join Date
    Jun 2005
    Location
    Whittington, Lancashire
    Posts
    286
    Thank Post
    40
    Thanked 36 Times in 26 Posts
    Rep Power
    26
    Here's a vbscript I use to delete log files. I'm sure you can tweak it for your needs:

    '================================================= =========================
    '
    ' NAME: DeleteLogFiles.vbs
    '
    ' AUTHOR: Jim Williams
    ' DATE : 07/08/2006
    '
    ' COMMENT: Deletes log files (or any other type) by age. Uses the file
    ' creation date to specify file to delete.
    '================================================= =========================
    Option Explicit

    ' Set constants for log file paths and maximum age of files
    const LogPath1 = "\\server6\c$\winnt\system32\logfiles\w3svc1"
    const LogPath2 = "\\server6\c$\WINNT\system32\LogFiles\W3SVC3"
    Const LogFileAge = 30

    'WScript.Echo LogPath1
    DeleteFilesByDate logpath1, LogFileAge
    'WScript.Echo logpath2
    DeleteFilesByDate logpath2, LogFileAge


    Sub DeleteFilesByDate(ByVal path, ByVal age)
    ' Deletes files over a certain age
    Dim objFSO
    Dim objFolder
    Dim colFiles
    Dim objFile

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(path)
    Set colFiles = objFolder.Files
    'WScript.Echo "More than " & age & " days old:"
    For Each objFile In colFiles
    ' If file is over age then delete
    If (objFile.DateCreated < Date - age) Then
    'WScript.Echo "Deleting..."
    'WScript.Echo objFile.Name & ": " & objFile.DateCreated
    objFile.Delete
    End If
    Next
    ' Tidy up
    Set objFSO = Nothing
    Set objFolder = Nothing
    Set colFiles = Nothing
    End Sub

  8. #7
    SkywOrca's Avatar
    Join Date
    Sep 2008
    Posts
    90
    Thank Post
    1
    Thanked 18 Times in 17 Posts
    Rep Power
    48
    Correct me if I'm wrong, but it looks as if the OP was simply looking for a tweak to his original script that caters for folders as well as files. He was almost there, the only thing he needs to add to the end of his script to achieve what he's looking for is something along the lines of:

    Code:
    Set colSubFolds = folders.SubFolders
    For Each objSubFold In colSubFolds
        If objSubFold.DateLastModified < dropdate Then
            objSubFold.Delete
        End If
    Next
    It will delete folders, and any content, if the last modified date is older than the dropdate. You might need to tweak around with the date comparison test (I've only briefly tested this, but it should work in the same manner as the files test you already listed). Hope this helps.

SHARE:
+ Post New Thread

Similar Threads

  1. Script to delete specific folders
    By Baasha_Bhai in forum Scripts
    Replies: 8
    Last Post: 2nd July 2008, 10:41 AM
  2. VB script to delete certain folders
    By PTD in forum Scripts
    Replies: 7
    Last Post: 4th June 2008, 12:30 PM
  3. Outlook 2007 - no email older than 90 days?
    By theeldergeek in forum How do you do....it?
    Replies: 8
    Last Post: 13th May 2008, 09:38 AM
  4. Script to delete certain folders
    By Pumaedition in forum Scripts
    Replies: 4
    Last Post: 22nd April 2008, 10:08 PM
  5. Replies: 5
    Last Post: 9th March 2008, 12:15 AM

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
  •