Solved

Script to find and delete files based on age

Posted on 2013-06-24
8
263 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 51

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 51

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 51

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to count occurrences of each item in an array.

708 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

14 Experts available now in Live!

Get 1:1 Help Now