We help IT Professionals succeed at work.
Get Started

VB Script to delete files based on age not working

Last Modified: 2019-11-21
Script was working on another PC. I moved a surveillance system to a new PC and I want to schedule a script to delete files older than 20 days to maintain disk space.

The system stores video in E:\Record. There are 4 subfolders called Cam01, Cam02 etc..

This is the script:

' Specify base folder to purge, and age in days of files to keep
Const strDir = "E:\Record"
Const intAge = 20

' Create file system object
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

' Remove old files recursively
RemoveOldFiles objFSO.GetFolder(strDir)

Sub RemoveOldFiles(objFolder)
    On Error Resume Next

    ' If no files of subfolders exist on first check, leave it alone
    If objFolder.Files.Count = 0 And objFolder.Subfolders.Count = 0 Then
        Exit Sub
    End If

    ' Remove any files older than specified days from this folder
    For Each objFile In objFolder.Files
        intFileAge = DateDiff("d", objFile.DateLastModified, Now)
        If intFileAge > intAge Then
            Wscript.Echo "Deleted file: """ & objFile.Path & """ Age: " & intFileAge & """."
        End If

    ' See if we were able to access this folder, if not don't recurse into it
    If Err.Number = 0 Then
        ' Remove all older files in any subfolders of this one
        For Each objSubFolder In objFolder.Subfolders
            RemoveOldFiles objSubFolder

        ' Report error if anable to access a folder
        Wscript.Echo "Error: """ & Err.Number & """ accessing folder: " & objFolder.Path & """."
        Exit Sub
    End If

    ' If folder is now empty, we purged its contents, so remove it
    If objFolder.Files.Count = 0 And objFolder.Subfolders.Count = 0 Then
        Wscript.Echo "Removing empty folder: """ & objFolder.Path & """."
    End If
End Sub

Open in new window

When the script runs it looks like it is deleting the video files as the attached screenshot shows, but they are not deleted and there is an error at the end of the deletion run for each Cam0x subfolder, also on the screenshot.

Any dies? The logged on user is an admin.
Watch Question
Most Valuable Expert 2012
Top Expert 2014
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE