FileSystemObject Folder.Delete returns "Path not found" for really long paths
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: FileSystemObject Folder.Delete returns "Path not found" for really long paths

  1. #1
    Join Date
    Sep 2000
    Location
    UK
    Posts
    122

    FileSystemObject Folder.Delete returns "Path not found" for really long paths

    I have this code which i run with different values for sPath

    Code:
    Dim oFSO As new Scripting.FileSystemObject
    Dim oFolder As Scripting.Folder
    Set oFolder = oFSO.GetFolder(sPath)
    Call oFolder.Delete(True)
    This works well until it ran on several paths...when I get the following error
    Error 76, "Path not found"

    I have obviously checked and the paths do exist, and i do have permissions to read/delete them

    I believe that the command tries to delete the folder, but firsts tries to delete the file under it, and this file has a absolute path which is over 256 characters long, which seams to exceed some limit in windows and prevents me from deleting the folder.

    Any Ideas how I can delete the directory (recursively) without getting this problem?
    All code on this page is protected by an SEP field!!!

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,877

    Re: FileSystemObject Folder.Delete returns "Path not found" for really long paths

    Maybe converting it to its short name first? This will give it an 8/11 DOS notation, which should work for you.

    Code:
    Option Explicit
    
    Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
    Private Declare Function GetFullPathName Lib "kernel32" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As Long
    
    Public Function GetShortPath(strFileName As String) As String
        'KPD-Team 1999
        'URL: http://www.allapi.net/
        'E-Mail: KPDTeam@Allapi.net
        Dim lngRes As Long, strPath As String
        'Create a buffer
        strPath = String$(165, 0)
        'retrieve the short pathname
        lngRes = GetShortPathName(strFileName, strPath, 164)
        'remove all unnecessary chr$(0)'s
        GetShortPath = Left$(strPath, lngRes)
    End Function
    
    Function GetFullPath() As String
        'KPD-Team 2000
        'URL: http://www.allapi.net/
        'E-Mail: KPDTeam@Allapi.net
        Dim Buffer As String, Ret As Long
        'create a buffer
        Buffer = Space(255)
        'copy the current directory to the buffer and append 'myfile.ext'
        Ret = GetFullPathName("Project1.vbp", 255, Buffer, "")
        'remove the unnecessary chr$(0)'s
        GetFullPath = Left(Buffer, Ret)
    End Function
        
    Private Sub Form_Load()
      Dim snm$, lnm$
      snm = GetShortPath("D:\Visual Basic Samples\G\GetShortPath\")
      Debug.Print snm
      lnm = GetFullPath
      Debug.Print lnm
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,771

    Re: FileSystemObject Folder.Delete returns "Path not found" for really long paths

    Have you tried to pass the Short-pathname to the function..

    Here is a quick function to get the short pathname using a Windows API..
    Code:
    Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal 
     
    lpszShortPath As String, ByVal cchBuffer As Long) As Long
    Public Function GetShortPath(ByVal strLongPath As String) As String
    	Dim sLongPath  As String
    	Dim sShortPath As String
    	Dim lPathLen   As Long
    	Dim lLen	   As Long
    	
    	sShortPath = Space$(255)
    	lLen = Len(sShortPath)
    	lPathLen = GetShortPathName(strLongPath, sShortPath, lLen)
    	GetShortPath = Left$(sShortPath, lPathLen)
    	
    End Function
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,771

    Re: FileSystemObject Folder.Delete returns "Path not found" for really long paths

    Hmm you beat me to it David...

    Good one
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    Sep 2000
    Location
    UK
    Posts
    122

    Unhappy Re: FileSystemObject Folder.Delete returns "Path not found" for really long paths

    Thanks for your quick replies, however they do not solve my problem as,
    I have tried ShortPath, but it still does not work as the paths of the files under the folder i am trying to delete push it back over 256....

    Even if I were to write a recurssive delete function and use ShortPaths all the way, it is still a potential to hit the 256 character limit as it may be many many folders deep
    All code on this page is protected by an SEP field!!!

  6. #6
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,877

    Re: FileSystemObject Folder.Delete returns "Path not found" for really long paths

    There may be a 255 character limit to Windows filenames, though. One of the folders may not be a valid Windows folder, so I don't know if the shortname will work. Let us know!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Sep 2000
    Location
    UK
    Posts
    122

    Re: FileSystemObject Folder.Delete returns "Path not found" for really long paths

    Hmm, interesting.

    The problem comes from that the folder is on a network drive.
    They were created on a share, which was shallow.
    However I need to access them from a lower level share, and this pushes the paths over the 256 limit.
    Some of the subfolders are not accessible from this share...and are in this context as you say Not "valid Windows folders".
    All code on this page is protected by an SEP field!!!

  8. #8
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    Re: FileSystemObject Folder.Delete returns "Path not found" for really long paths

    Something you could try to do is, with the root path you will start from (or each lowest level folder), shell a SUBST (DOS) command to specify that path as it's own drive, then use the substitute drive in the FSO and go from there.
    Code:
    Associates a path with a drive letter.
    
    SUBST [drive1: [drive2:]path]
    SUBST drive1: /D
    
      drive1:        Specifies a virtual drive to which you want to assign a path.
      [drive2:]path  Specifies a physical drive and path you want to assign to
                     a virtual drive.
      /D             Deletes a substituted (virtual) drive.
    
    Type SUBST with no parameters to display a list of current virtual drives.
    Using SUBST allows you to bypass Windows internal limitations for file name and folder path lengths, and SUBST works on shared network folders as well.

  9. #9
    Join Date
    Sep 2000
    Location
    UK
    Posts
    122

    Re: FileSystemObject Folder.Delete returns "Path not found" for really long paths

    That looks like it might work..
    I will try it tomorrow and let you know the result
    Thanks
    All code on this page is protected by an SEP field!!!

  10. #10
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,877

    Re: FileSystemObject Folder.Delete returns "Path not found" for really long paths

    Could just map them, also. As in NET USE /?.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Mar 2013
    Posts
    1

    Thumbs up Re: FileSystemObject Folder.Delete returns "Path not found" for really long paths

    Do you have error messages?, like :
    Path too long
    Error cannot delete file: cannot read from source file or disk
    Cannot delete file: Access is denied
    There has been a sharing violation.
    Cannot delete file or folder The file name you specified is not valid or too long. Specify a different file name.
    The source or destination file may be in use.
    The file is in use by another program or user.
    Error Deleting File or Folder
    Make sure the disk is not full or write-protected and that the file is not currently in use.
    Error Copying File or Folder.
    Cannot remove folder.
    The filename or extension is too long.
    Path too deep.
    Destination Path Too Long.
    Could not find this item.
    Filename is not valid.
    The file could not be accessed.
    Windows Delete Path Too Long
    Source Path Too Long Delete
    The path you entered, is too long. Enter a shorter path.
    File Name could not be found. Check the spelling of the filename, and verify that the file location is correct.

    You may used Long Path Tool (which I currently used). It is easy to used and very suitable to such kind of problem


    you can find this one at http://longpathtool.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center