Hi

I am trying to run the code below and get

Exception From Hresult 0x800A0046
(CTL_E_PermissionDenied) error only on Button 2. 

Button 1 does what it is supposed to ie remove all files found in the folders that are older than today. Button 2 should remove all files regardless but it generates the Exception.

Here is the code

'Button 1

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 fldr
        Dim i As Integer

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

                If fso.FolderExists(pathstring) = True Then

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

                        End If

                    Next

                Else
                    MsgBox("Folder does not exist")
                End If

            Next
         i=i+1
        End While

And

'Button 2

    End SubPrivate 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 fldr
        Dim i As Integer

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

                If fso.FolderExists(pathstring) = True Then

                    For Each file In fso.GetFolder(pathstring).f
                            fso.DeleteFile(file)


                    Next

                Else
                    MsgBox("Folder does not exist")
                End If

            Next
           i=i+1
        End While

        MsgBox("Action Completed Successfully")

    End Sub

Thanks

'The following line is the problem. The .f is not a valid member.
For Each file In fso.GetFolder(pathstring).f

Why not use the following instead of your approach

For Each sFolder As String In VideoFolders
If My.Computer.FileSystem.DirectoryExists(sFolder) Then
For Each sFile As String In My.Computer.FileSystem.GetFiles(sFolder)
System.IO.File.Delete(sFile)
Next
End If
Next

You are showing 2 button3_click but no button2_click.

What is or where is Button2 code?
I didn't see it in the post

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.