I am using Visual Basic Developer 2005

I have this simple code that will delete all files created before today in the specified folders.

The code works fine but I get an exception problem when the fso object deletes the files ie at fso.DeleteFile(file) inside the second For loop. I have marked this line Red in the code below.

At this point execution stops. On checking the folders and carrying out some tests with the system calender I find that this code does its job without a problem.

Here is the Exception message
Exception from HRESULT: 0x800A0046 (CTL_E_PERMISSIONDENIED)

Here is the code

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim fso
        Dim pathstring
        Dim file
        Dim VideoFolders(4) As String
        Dim CustomerTable As New DataTable
        Dim fldr
        Dim i As Integer

        'array initialization

        VideoFolders(0) = "C:\001310da9f32\"
        VideoFolders(1) = "C:\001310daa2fc\"
        VideoFolders(2) = "C:\001310daa22a\"
        VideoFolders(3) = "C:\001310db0363\"

        'Initialize FileSystemObject

        fso = CreateObject("Scripting.FileSystemObject")

        'Initialize While Loop Variable
        i = 0

        While i < 4

            For Each fldr In VideoFolders(i)

                pathstring = fso.GetAbsolutePathName(VideoFolders(i))
                i = i + 1
                If fso.FolderExists(pathstring) = True Then

                    For Each file In fso.GetFolder(pathstring).files
                        If DateValue(file.datelastModified) < Date.Today Then

                        End If


                    MsgBox("Folder does not exist")
                End If

        End While

        MsgBox("Action Completed Successfully")

    End Sub


I found the mistake i = i + 1 should go on the line before end while this solves the exception problem not where I put it originally.


This question has already been answered. Start a new discussion instead.