• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 744
  • Last Modified:

vbs script to delete files and folders that are older then 24hrs

hi all

im looking for a script that will delete old files and folders from some temp locations we have and a couple of other locations.

i have found the code below on this site, which is great for deleting files older then 24hrs.
but it doesnt remove the folders.

my vbs scripting knowledge is very poor so i have no idea how to modify this script so it removes the old folders as well.

or does anybody have a script that i can run after this one that will remove the folders as well??


thanks!




strRoot = "c:\VMLocal\backup"
strMask = "*.*"
blnSubFolders = True
intHours = 24
 
strCommand = "cmd /c @echo off & dir " & Chr(34) & _
    strRoot & "\" & strMask & Chr(34) & " /a:-d /b"
    
If blnSubFolders Then
    strCommand = strCommand & " /s"
End If
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objWshScriptExec = objShell.Exec(strCommand)
Set objStdOut = objWshScriptExec.StdOut
 
Do Until objStdOut.AtEndOfStream
    strFile = objStdOut.ReadLine
    
    If Not blnSubFolders Then
        strFile = strRoot & "\" & strFile
    End If
    
    Set objFile = objFSO.GetFile(strFile)
    
    If DateDiff("h", objFile.DateLastModified, Now) > intHours Then
        WScript.Echo "Delete " & strFile
        'objFile.Delete True
    End If
Loop

Open in new window

0
BakerSyd
Asked:
BakerSyd
1 Solution
 
prashanthdCommented:
Hi,

Try the following code

regards
Prashanth
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "c:\VMLocal\backup"

on error resume next
Set objFolder = objFSO.GetFolder(objStartFolder)
WScript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile In colFiles
    If DateDiff("h", objFile.DateLastModified, Now) > 24 Then
        WScript.Echo objFile.Name & " Delete"
        objFile.Delete True
    End If
Next
WScript.Echo

ShowSubfolders objFSO.GetFolder(objStartFolder)

Sub ShowSubFolders(Folder)
    For Each Subfolder In Folder.SubFolders
        WScript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        If DateDiff("h", objFolder.DateLastModified, Now) > 24 Then
            WScript.Echo objFolder.Name & " Delete"
            objFolder.Delete True
        Else
            Set colFiles = objFolder.Files
            For Each objFile In colFiles
                If DateDiff("h", objFile.DateLastModified, Now) > 24 Then
                    WScript.Echo objFile.Name & " Delete"
                    objFile.Delete True
                End If
            Next
        End If
        WScript.Echo
    Next
End Sub

Open in new window

0
 
BakerSydAuthor Commented:
excellent script, does exactly what i need

thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now