none
Unable to Delete Folder from Network path using VB 2010 code RRS feed

  • Question

  • Hi,

    I am unable to delete Folder from Shared Loaction using VB2010 Code.

    It is throwing Access Denied Exception. Please find the below error details for your reference.

    But I am able to delete Files using Vb 2010 code. And I am able to create Folder as well using code

    Error Description: 

    System.Security.SecurityException was unhandled by user code
      Message=Exception from HRESULT: 0x800A0046 (CTL_E_PERMISSIONDENIED)
      Source=""
      StackTrace:
           at Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn)
           at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
           at ST_3a48db4e6bb9413291092e17c96045c6.ScriptMain.Main() in C:\Users\C5061125\AppData\Local\Temp\2\vsta\e60b6969794242bbb218157aea313ba8\ScriptMain.vb:line 92
      InnerException: 

    Please find the required details below,

    Share Location Path: \\Server Ip\Aloca_Garn\ALCOA_GARN_DE\UNZIPPED\

    Vb 2010 Code:

            Dim fSrc
            Dim fsoFile
            fSrc = \\Server Ip\Aloca_Garn\ALCOA_GARN_DE\UNZIPPED\
            fsoFile = CreateObject("Scripting.FileSystemObject")
            fsoFile.DeleteFolder(fSrc, True)

    Please help

    Thanks

    Kathiravan

    Tuesday, August 8, 2017 7:50 AM

All replies

  • Access rights to any resource  controlled by the Windows O/S is based on user account permissions on said resource. Your desktop Windows program runs under the context of the logged in user account on machine. If the logged in user account doesn't have the permissions on the resource, then it's access denied
    • Proposed as answer by Frank L. Smith Tuesday, August 8, 2017 11:50 AM
    Tuesday, August 8, 2017 10:50 AM
  • Thank you for the response,

    Application running under my user account and I have full access as well.

    Here the problem is File.DeleteFolder() throws Access Denied exception eventhough that folder is empty.

    Could you please help me on this

    Tuesday, August 8, 2017 1:48 PM
  • Thank you for the response,

    Application running under my user account and I have full access as well.

    Here the problem is File.DeleteFolder() throws Access Denied exception eventhough that folder is empty.

    Could you please help me on this

    Just because you have full control with your account, it doesn't mean that an account that has lessor permissions is not superseding your permission on a resource, users/machinename users is such an account. It doesn't seem that at the folder level, your account has permissions and some other account may be superseding your account. 

    Tuesday, August 8, 2017 2:16 PM
  • Thank you once again,

    Could you please suggest any solution for this.

    Now I have updated my code mentioned below, eventhough facing the same issue.

      Dim fsoFile, fldMainFolder, fldZipMainFolder, fldFolder
            Dim fldZipFolder, fldZipSubFolder
            Dim fileName, fSubName, fFile, fldName

            fsoFile = CreateObject("Scripting.FileSystemObject")
            fldMainFolder = fsoFile.GetFolder(Dts.Variables("gvRecFolder").Value)
            fldZipFolder = fldMainFolder.SubFolders

            For Each fldName In fldZipFolder
                If fldName.Files.Count = 0 Then
                    fldName.Delete()
                End If

                Exit For
            Next
            fsoFile = Nothing
            fldMainFolder = Nothing
            fldZipFolder = Nothing
            fldName = Nothing

            Dts.TaskResult = ScriptResults.Success
        End Sub

    Tuesday, August 8, 2017 2:24 PM
  • Hello Kathiravan,

    That is not VB2010 but VB scripting code which you try to use in VB, in vintage VB that was possible.

    However, in VB 2010 it is much easier to delete a directory

    description and samples on this page. 

    https://msdn.microsoft.com/en-us/library/62t64db3(v=vs.110).aspx

    Be aware that you never can delete a directory if you cannot delete it using file explorer too.


    Success
    Cor


    Tuesday, August 8, 2017 3:15 PM
  • Hi Cor,

    Thank you for your response,

    I have tried as per your suggestion, Please find the updated code below.

    Dim fsoFile, fldMainFolder, fldZipMainFolder, fldFolder
            Dim fldZipFolder, fldZipSubFolder
            Dim fileName, fSubName, fFile, fldName

            fsoFile = CreateObject("Scripting.FileSystemObject")
            fldMainFolder = fsoFile.GetFolder(Dts.Variables("gvRecFolder").Value)
            fldZipFolder = fldMainFolder.SubFolders

            For Each fldName In fldZipFolder
                If fldName.Files.Count = 0 Then
                    Dim fldPath As String
                    fldPath = Dts.Variables("gvRecFolder").Value + fldName.Name
                    Directory.Delete(fldPath)
                End If

                Exit For
            Next
            fsoFile = Nothing
            fldMainFolder = Nothing
            fldZipFolder = Nothing
            fldName = Nothing

    Now I am getting this error "The directory is not empty" now. I have cleared this folder by deleting subfolder just before this code execution.

    In the debugging mode after crossed the deletion on subfolder successfully, I saw that subfolder exist inside at runtime, but after stopped the execution, the subfolder disappered.

    Error Description:

    System.IO.IOException was unhandled by user code
      Message=The directory is not empty.

      Source=mscorlib
      StackTrace:
           at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
           at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)
           at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)
           at System.IO.Directory.Delete(String path)
           at ST_3a48db4e6bb9413291092e17c96045c6.ScriptMain.Main() in C:\Users\C5061125\AppData\Local\Temp\2\vsta\a9bb589a53164f4e9bfd543890e04483\ScriptMain.vb:line 114
      InnerException: 

    Please help me on this

    Tuesday, August 8, 2017 3:54 PM
  • Hi Cor,

    Thank you for your response.

    I have tried as per your suggestion. Please find the updated code below.

    Dim fsoFile, fldMainFolder, fldZipMainFolder, fldFolder
            Dim fldZipFolder, fldZipSubFolder
            Dim fileName, fSubName, fFile, fldName

            fsoFile = CreateObject("Scripting.FileSystemObject")
            fldMainFolder = fsoFile.GetFolder(Dts.Variables("gvRecFolder").Value)
            fldZipFolder = fldMainFolder.SubFolders

            For Each fldName In fldZipFolder
                If fldName.Files.Count = 0 Then
                    Dim fldPath As String
                    fldPath = Dts.Variables("gvRecFolder").Value + fldName.Name
                    Directory.Delete(fldPath)
                End If

                Exit For
            Next
            fsoFile = Nothing
            fldMainFolder = Nothing
            fldZipFolder = Nothing
            fldName = Nothing

    Now I have received this error "The directory is not empty". I have cleared by this folder deleting subfolder as like this just before this code execution. 

    In debugging mode, After crossed the deletion code for subfolder successfully,I saw that sub folder was existing at runtime. But after stopped the execution subfolder got disappeared.

    Error Description:

    System.IO.IOException was unhandled by user code
      Message=The directory is not empty.

      Source=mscorlib
      StackTrace:
           at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
           at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)
           at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)
           at System.IO.Directory.Delete(String path)
           at ST_3a48db4e6bb9413291092e17c96045c6.ScriptMain.Main() in C:\Users\C5061125\AppData\Local\Temp\2\vsta\a9bb589a53164f4e9bfd543890e04483\ScriptMain.vb:line 114
      InnerException: 

    Please help me on this.

    thanks,

    Kathiravan

    Tuesday, August 8, 2017 4:06 PM
  • Hi KathiravanRajeswaran,

    Since this forum is discussing and asking questions about the Visual Basic programming language, IDE, libraries, samples, and tools . And your issue is more related to VBScript. This link about VBScript, please refer that:

    https://social.technet.microsoft.com/Forums/scriptcenter/en-us/home?forum=ITCG


    Thank you for participating in the forum activities.


    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, August 9, 2017 6:50 AM
    Moderator