Solved

Script to find and delete files based on age

Posted on 2013-06-24
8
266 Views
Last Modified: 2013-06-25
I need a turnkey Windows script that can scan a folder with subfolders and delete any files named *.upload that are older than 24 hours.  The script also needs to output the files deleted to a log file.
0
Comment
Question by:murryc
  • 3
  • 3
  • 2
8 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39273436
Give this a try in a testing area first.  Just adjust the line GetFiles("C:\stuff\pics") with the initial folder/subfolder you want to start in.  After that, it should go through each folder and subfolder.

Dim fso
Dim ObjOutFile
 
'Creating File System Object
Set fso = CreateObject("Scripting.FileSystemObject")
 

'Call the GetFile function to get all files
GetFiles("C:\stuff\pics")
 
'Close the output file
ObjOutFile.Close
 
WScript.Echo("Completed")
 
Function GetFiles(FolderName)
    On Error Resume Next
     
    Dim ObjFolder
    Dim ObjSubFolders
    Dim ObjSubFolder
    Dim ObjFiles
    Dim ObjFile
 
    Set ObjFolder = fso.GetFolder(FolderName)
    Set ObjFiles = ObjFolder.Files
     
    'Write all files to output files
    For Each ObjFile In ObjFiles
        if ObjFile.DateLastModified > dateadd("h",24,now()) then
            if ObjFile.GetExtensionName="upload" then
                ObjFile.DeleteFile(ObjFile.Path&"\"&ObjFile.Name) ' dleete the file
            end if
        end if
    Next
     
    'Getting all subfolders
    Set ObjSubFolders = ObjFolder.SubFolders
     
    For Each ObjFolder In ObjSubFolders
        'Getting all Files from subfolder
        GetFiles(ObjFolder.Path)
    Next
     
End Function

Open in new window

0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39273498
Not sure what happened, I know I posted this to EE hours ago, but it's gone now.  Unless I put it on the wrong question, you had a few going...

Const ForAppending = 8

' Define files and folders to process
strBaseDir = "C:\EE\EE28166356\Files"
strBaseExt = ".upload"
strLogName = "C:\EE\EE28166356\logfile.txt"
intAgeHours = 24

' Define filesystem object, access folder to scan
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objBaseDir = objFSO.GetFolder(strBaseDir)

' Append to the log file
Set objLog = objFSO.OpenTextFile(strLogName, ForAppending, True)

' Check each file in the folder
For Each objFile in objBaseDir.Files
    ' See if it's the extension we want
    If LCase(Right(objFile.Name, Len(strBaseExt))) = LCase(strBaseExt) Then
        ' Check it's age, se if ready to delete
        If DateDiff("h", objFile.DateLastModified, Now) > intAgeHours Then
            ' Old enough, log it and delete it
            objLog.WriteLine "DELETE File:[" & objFile.Path & "], Age:[" & DateDiff("h", objFile.DateLastModified, Now) & "] hours"
            objFile.Delete
        End If
    End If
Next

' Close log file, release objects
objLog.Close()
Set objLog = Nothing
Set objFSO = Nothing

Open in new window

~bp
0
 

Author Comment

by:murryc
ID: 39273616
padas...error on line 12 char 1

billprew...works great on a single root folder, but it does not scan subfolders.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39273670
I copied and pasted and modified.  Just comment out that line or delete it.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39274408
Here's the recursive version to handle subfolders.

Const ForAppending = 8

' Define files and folders to process
strBaseDir = "C:\EE\EE28166356\Files"
strBaseExt = ".upload"
strLogName = "C:\EE\EE28166356\logfile.txt"
intAgeHours = 24

' Define filesystem object, access folder to scan
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Append to the log file
Set objLog = objFSO.OpenTextFile(strLogName, ForAppending, True)

' Call subroutine to recursively delete old files from folder and all subfolders
DeleteOldFiles objFSO.GetFolder(strBaseDir)

' Close log file, release objects
objLog.Close()
Set objLog = Nothing
Set objFSO = Nothing

Sub DeleteOldFiles(objFolder)
    ' Check each file in the folder
    For Each objFile in objFolder.Files
        ' See if it's the extension we want
        If LCase(Right(objFile.Name, Len(strBaseExt))) = LCase(strBaseExt) Then
            ' Check it's age, se if ready to delete
            If DateDiff("h", objFile.DateLastModified, Now) > intAgeHours Then
                ' Old enough, log it and delete it
                objLog.WriteLine "DELETE File:[" & objFile.Path & "], Age:[" & DateDiff("h", objFile.DateLastModified, Now) & "] hours"
                objFile.Delete
            End If
        End If
    Next

    ' Check each subfolder in the folder
    For Each objSubFolder in objBaseDir.SubFolders
        DeleteOldFiles objSubFolder
    Next
End Sub

Open in new window

~bp
0
 

Author Comment

by:murryc
ID: 39275294
padas..I get a complete message after commenting out that line but the .upload files do not get deleted.

bp...I get the following error on your code...

Line 38, char 5
Object required: 'objBaseDir'
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39275531
Sorry, please change:

For Each objSubFolder in objBaseDir.SubFolders

to:

For Each objSubFolder in objFolder.SubFolders

~bp
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39275565
Change  original line 31

from

if ObjFile.GetExtensionName="upload" then

to

 if lcase(right(ObjFile.name,6))="upload" then
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now