VB Script to delete files based on age not working

snooflehammer
snooflehammer used Ask the Experts™
on
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 & """."
            objFile.Delete
        End If
    Next

    ' 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
        Next

    Else
        ' 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 & """."
        objFolder.Delete
    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.
Annotation-2019-11-21-112824.jpg
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi, firstly, comment out On Error Resume Next and run the script again.  Chances are you don't have permission to delete the files.
If you run a command prompt "As Administrator", then type
cscript C:\Path\yourscript.vbs

that would run the script with elevated rights, so you can try that as well.

Regards,

Rob.

Author

Commented:
Didn't help I'm afraid.

If I comment that line out the script just doesn't run.

If I run it from the command prompt I get a pop-up per file asking me to OK deletion, but then it doesn't actually delete.

All users have Full Control over the files & folders.

I can delete file via right-click | Delete
Most Valuable Expert 2012
Top Expert 2014

Commented:
If you are getting a popup, that will be the Wscript.Echo statement, which means you aren't running the script from a command prompt using
cscript C:\Path\Yourscript.vbs

If you run it that way, with On Error Resume Next commented out, you should see an error that would indicate why the script isn't doing what it's supposed to do.  Can you post the output from the command prompt once you try to run the script that way?

Rob.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

Commented:
I see that I didn't read your instructions properly & I had omitted the cscript bit.

I get the following output at the command line:

e:\record\deleteoldvideo.vbs (24, 13) Microsoft VBscript runtime error: permission denied

line 24 is objfile.delete

Permissions are wide open on the whole volume - Everyone | Full Control
Most Valuable Expert 2012
Top Expert 2014
Commented:
OK cool.  Instead of
objFile.Delete

can you try
objFSO.DeleteFile objFile.Path, True

Rob.

Author

Commented:
Rock & Roll

You're the man
Most Valuable Expert 2012
Top Expert 2014

Commented:
Great! I've not had much success with calling the .Delete method directly from a file object, but knowing that's where the error was helped, so .DeleteFile works better.

Rob.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial