Solved

Script to find and delete files based on age

Posted on 2013-06-24
8
270 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 53

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
How our DevOps Teams Maximize Uptime

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

 
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
 
LVL 53

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 53

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Run a .bat file wich is inside a .vbs file 8 50
Script to change all permissions to my user 7 44
Clear Filter 8 59
Use GPO script to report finding a certain file 16 15
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

839 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