Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Script to find and delete files based on age

Posted on 2013-06-24
8
Medium Priority
?
275 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 54

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 58

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 54

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 58

Accepted Solution

by:
Bill Prew earned 2000 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 58

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 54

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

618 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