Solved

Script to find and delete files based on age

Posted on 2013-06-24
8
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 53

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 55

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
Stressed Out?

Watch some penguins on the livecam!

 
LVL 53

Expert Comment

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

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 55

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 53

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

691 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