+ Reply to Thread
Results 1 to 17 of 17

Working with Paths over 255 characters - file count

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Working with Paths over 255 characters - file count

    Hi All,

    I'm having some problems coming up with a work around for paths over the maximum character length. At present I'm getting a Runtime 76 - Path Not Found error.

    I know the ideal solution would be to get the excesive folder structure corrected but the user base is large and there are other dependacies that don't really make that a viable solution.

    I'm using the following function to count the number of files in folders / subfolders which is then used in a progress bar:
    Function GetFileCount(localRoot, Optional fld, Optional count As Long) As Long
    
    Dim baseFolder As Object, subFolder As Object
    
        Set fso = CreateObject("Scripting.Filesystemobject")
    
        If IsMissing(fld) Then
            Set baseFolder = fso.getfolder(localRoot)
        Else
            Set baseFolder = fld
        End If
            
        count = count + baseFolder.Files.count
        
        For Each subFolder In baseFolder.SubFolders
            GetFileCount localRoot, subFolder, count
        Next
    
        GetFileCount = count
        
    End Function
    As I said all works fine until it encounters lengthy paths.

    I've done some research into workarounds but having trouble understanding how they work in order to integrate into this code.
    I've seen something about adding prefixes to the file path to cope with longer paths, "\?\" for relative and "\\?\unc\" for absolute paths (https://stackoverflow.com/questions/...onger-than-260).
    And seen something called ShortPath which makes the filepath unreadable to the user but not an issue here (https://stackoverflow.com/questions/...paths-over-260).
    For the life of me I can't seem to get either method integrated into this function - I'm sure that's down to my own ignorance but I have tried everything I can think of here.

    Or I'm open to an alternative way of counting the number of files that doesn't hit an issue with long paths.

    As always, any assistance appreciated.

    Thanks, TC

  2. #2
    Valued Forum Contributor
    Join Date
    11-02-2016
    Location
    NY
    MS-Off Ver
    2010
    Posts
    459

    Re: Working with Paths over 255 characters - file count

    added 11-8-17
    Drive Mapping.jpg ~ what I was hoping to do is bypass the long folder names my Drive Mapping (you may want to check with your local IT dept?) but you can Brows the folders to get to your destination. So, the drive letter Z could be designated for a long file path. I did see an article (Below) as I'm not an expert either in fiddling with FSO. Keep at it, sounds like your very close!

    just found something ~ https://stackoverflow.com/questions/...onger-than-260

    older suggestion:
    Can you 'Map' the drive? So, if your path is: S:\serverName\folder1\folder2\folder3\folder4\folder5\LookyHere ~ can you map a new drive letter "P:" that would take the place for everything up to & including the LookyHere...then you can just reference drive P. It's worth a shot?!
    Last edited by queuesef; 11-08-2017 at 11:23 AM.

  3. #3
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Working with Paths over 255 characters - file count

    I think I kind of get what you're saying here but I have no experience with this so not sure entirely how it would work.
    I'm not specifying the path directly - the FSO is picking that up - and that's the problem I'm having; how to amend the path either via adding Unicode (?) prefixes or using ShortPath. I can't work out how to integrate that into the code.

  4. #4
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Working with Paths over 255 characters - file count

    Hi Queuesef,

    Thanks for the more detailed explanation - I had started going down this route before xlnitwit pointed me in the right direction with the shortpath. There were some issues with it though.
    If the user already has that drive letter mapped for example. And how to incorirate into recursive code - could end up exhausting the number of drive letters availiable.

    So for me ShortPath was better. The only issue I have now - is how to maintain the file path in full for communicating back to the user. I'll post an update below with the code I'm using to check certain file attributes.

  5. #5
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Working with Paths over 255 characters - file count

    This is getting even stranger... It looks like the path lengths might not actually be what is causing the error, rather that VBA can't even see files / folders with paths over the max length.

    I set up a dummy folder structure with about 6 or 7 sub folders each with a long name and placed files in each. Windows does try to stop you doing this but it lets you rename a folder to a longer name.

    I then run the following code:
    Sub LongPathTest()
    
    Dim MainFolderName As String
    Dim FileCount As Long
    
        'Set objShell = CreateObject("Shell.Application")
       
        'Provide user interface for location selection
        MainFolderName = BrowseForFolder()
       
        'Check if user cancelled or otherwise didn't make a valid selection; warn and exit
        If MainFolderName = "Invalid" Then
            MsgBox "Please select a valid location / folder you wish to start the self-test on." & vbNewLine & _
            "Start over by clicking on the 'Start Self-Test' button", vbExclamation, "Invalid Selection"
            GoTo Abort
        Else
            'Count the number of files in the chosen location and update the progress bar
            FileCount = GetFileCount(MainFolderName)
            MsgBox FileCount & " Files in total", vbOKOnly, "Count"
        End If
    
    Abort:
    
    End Sub
    
    
    
    Function BrowseForFolder(Optional OpenAt As Variant) As String
         'Function purpose:  To Browser for a user selected folder.
         'If the "OpenAt" path is provided, open the browser at that directory
         'NOTE:  If invalid, it will open at the Desktop level
         
        Dim ShellApp As Object
         
         'Create a file browser window at the default folder
        Set ShellApp = CreateObject("Shell.Application"). _
        BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
         
         'Set the folder to that selected.  (On error in case cancelled)
        On Error Resume Next
        BrowseForFolder = ShellApp.self.Path
        On Error GoTo 0
         
         'Destroy the Shell Application
        Set ShellApp = Nothing
         
         'Check for invalid or non-entries and send to the Invalid error
         'handler if found
         'Valid selections can begin L: (where L is a letter) or
         '\\ (as in \\servername\sharename.  All others are invalid
        Select Case Mid(BrowseForFolder, 2, 1)
        Case Is = ":"
            If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
        Case Is = "\"
            If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
        Case Else
            GoTo Invalid
        End Select
         
    Exit Function '---------------------------------------------------------
         
    Invalid:
         'If it was determined that the selection was invalid, set to False
        BrowseForFolder = "Invalid"
         
    End Function
    
    
    Function GetFileCount(localRoot, Optional fld, Optional count As Long) As Long
    
    Dim baseFolder As Object, subFolder As Object, fso as object
    
        Set fso = CreateObject("Scripting.Filesystemobject")
    
        If IsMissing(fld) Then
            Set baseFolder = fso.getfolder(localRoot)
        Else
            Set baseFolder = fld
        End If
    
        count = count + baseFolder.Files.count
        
        For Each subFolder In baseFolder.SubFolders
            Debug.Print subFolder
            Debug.Print Len(subFolder)
            GetFileCount localRoot, subFolder, count
        Next
    
        GetFileCount = count
        
    End Function
    As I rename one of the top level folders to a longer name the less far down VBA appears to be able to see. Anything over the max length and it just doesn't register. But it's not giving me the same Runtime Error 76 Path Not Found as it if for other folders. I created my dummy structure on the same drive to be sure.

    It's worrying that VBA is simply not picking everything up, but giving no error - yet in other situations it is...? I'm guessing there must be something else causing the 76 error. But I can't think what.

    Anyone else replicating the same behaviour?

    TC

  6. #6
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Working with Paths over 255 characters - file count

    Further testing has revealed that if the path is exactly 259 characters long then the Runtime Error 76 occurs (presumably because on adding a "\" to look for files it hits the 260 limit [not 255 like I thought]) - anything over this just doesn't even get seen.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Working with Paths over 255 characters - file count

    Perhaps this
    Function GetFileCount(localRoot, Optional fld, Optional count As Long) As Long
    
    Dim baseFolder As Object, subFolder As Object
    
        Set fso = CreateObject("Scripting.Filesystemobject")
    
        If IsMissing(fld) Then
            Set baseFolder = fso.getfolder(localRoot)
        Else
            Set baseFolder = fso.getfolder(fld.shortpath)
        End If
            
        count = count + baseFolder.Files.count
        
        For Each subFolder In baseFolder.SubFolders
            GetFileCount localRoot, subFolder, count
        Next
    
        GetFileCount = count
        
    End Function
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  8. #8
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Working with Paths over 255 characters - file count

    Thanks xlnitwit - that is exactly what I was trying to do! but just couldn't work out how to integrate the 'shortpath'.

    It has left me with a subsequent issue - is there anyway to retrieve the long path back from the short path? Or maintain it in unison somehow?

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Working with Paths over 255 characters - file count

    Honestly, I don't know- I can't see how with this approach, but will see if there is another one that can be adapted.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Working with Paths over 255 characters - file count

    deleted ...
    Entia non sunt multiplicanda sine necessitate

  11. #11
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Working with Paths over 255 characters - file count

    This is the recursive code I'm using to check certain file attributes, this isn't the full code but a stripped down version to give you and idea of the structure and what I was thinking with trying to store the full path to a string:

    
    
    Sub RecursiveFolder2(xFolder, FileCount As Long, NewSht As Worksheet)
    
       
        '---store initial full path to string
        
        If xFolder.subfolders.count > 0 Then
        
            '---store folder in string and add to full path
            
            For Each SubFld In fso.getfolder(xFolder.shortpath).subfolders
                Set oFolder = fso.getfolder(SubFld)
                Set objFolder = objShell.Namespace(SubFld)
                
                For Each Fil In SubFld.Files
                    Set objFolder = objShell.Namespace(oFolder.path)
                    
                    'Test files
                    'Store FULL path (path and fodler strings) in array along with other relevant details
                        
                Next Fil
    
                Call RecursiveFolder(SubFld, FileCount, NewSht)
                
            Next SubFld
            
        Else
        
            '---remove last folder from string
        
        End If
    All objects seem to have the shortpath associated with them - which I guess makes sense. So I don't think there's anyway to obtain the full path again, e.g. from the Fil object.

    The problem I'm having when trying to store to a string is know when to delete the last folder and when to retain it, for the next sub (sub / subsub / subsubsub / etc) folder. I'm not sure this method would be possible either.

  12. #12
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Working with Paths over 255 characters - file count

    This seems to work in limited testing
    Sub RecursiveFolder2(xFolder, FileCount As Long, NewSht As Worksheet)
       Dim fso As Object
       Dim objShell As Object
       Set objShell = CreateObject("Shell.Application")
       Set fso = CreateObject("scripting.Filesystemobject")
        '---store initial full path to string
        
        If xFolder.subfolders.Count > 0 Then
        
            '---store folder in string and add to full path
            
            For Each SubFld In fso.getfolder(GetFolderPath(xFolder)).subfolders
                Set ofolder = fso.getfolder(GetFolderPath(SubFld))
                Set objfolder = objShell.Namespace(SubFld)
                
                For Each Fil In SubFld.Files
                    Set objfolder = objShell.Namespace(GetFolderPath(ofolder))
                    
                    'Test files
                    'Store FULL path (path and fodler strings) in array along with other relevant details
                    Debug.Print Replace$(ofolder.Path, "\\?\", "") & ": " & Len(Replace$(ofolder.Path, "\\?\", ""))
                Next Fil
    
                Call RecursiveFolder2(SubFld, FileCount, NewSht)
                
            Next SubFld
            
        Else
        
            '---remove last folder from string
        
        End If
    End Sub
    Function GetFolderPath(xFolder) As String
       Dim sPath As String
       sPath = xFolder.Path
       If Left$(sPath, 2) = "\\" Then
        If Left$(sPath, 3) <> "\\?" Then
        sPath = "\\?\UNC\" & Mid$(xFolder.Path, 3)
        End If
        Else
        sPath = "\\?\" & xFolder.Path
        End If
        GetFolderPath = sPath
    End Function

  13. #13
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Working with Paths over 255 characters - file count

    Okay so this is using the UNC method - I'm hitting a problem with it setting the objFolder - each time it's Nothing.

    Set objfolder = objShell.Namespace(GetFolderPath(ofolder))
    Here's a bit more of the code which will hopefully shed some light - I'm adapting existing code so don't fully understand all of what it's doing - I'm not certain that objFolder nor objFolderItem is even needed, they are public variables but I can't find them anywhere else in the subsequent code:
    Sub RecursiveFolder3(xFolder, FileCount As Long, NewSht As Worksheet)
    'Subroutine to extract all file names and associated properties from the sub folders in the location specified by the user
    'Called by: User > Start Self-Test > ProBarForm1 > MainExtractData
    
    Dim SubFld
        
        For Each SubFld In fso.getfolder(GetFolderPath(xFolder)).subfolders
            Set oFolder = fso.getfolder(GetFolderPath(SubFld))
            Set objFolder = objShell.Namespace(SubFld)
            
            For Each Fil In SubFld.Files
                Set objFolder = objShell.Namespace(GetFolderPath(oFolder))
                
                'Problem with objFolder at times
                If Not objFolder Is Nothing Then
                    Set objFolderItem = objFolder.ParseName(Fil.Name)
                    i = i + 1
                    If i Mod 20 = 0 Then
                        If PathErr = True Then
                            Application.StatusBar = "Processing File " & i - 1
                        Else
                            Application.StatusBar = "Processing File " & i - 1 & " / " & FileCount
                        End If
                        DoEvents
                        'If the user has clicked cancel then abort the code - delete created worksheet
                        If UserCancelled = True Then GoTo Abort
                    End If
                    
                    'Reset Variables
                    FailReason = ""
                    FailChar = ""
                    FailExt = ""
                    ValidVC = False
                    InvalidVC = False
                    ValidDateFormat = False
                    ValidDate = False
                    UpCase = False
                    LowCase = False
                    'UserCancelled = False
                    
                    X(i, 1) = Replace$(oFolder.path, "\\?\", "")
                    Debug.Print Replace$(oFolder.path, "\\?\", "") & ": " & Len(Replace$(oFolder.path, "\\?\", ""))
                    X(i, 2) = Fil.Name 'File Name
                    
                    '...
                    'Additional info includes Fil.DateLastModified, secDesc.owner (using ofolder.path and Fil.name)
                    'Tests performed on file to exclude certain locations and filetypes then testin the name to
                    'check it meets a naming convention
                    '...
                    
                Else
                    Debug.Print Fil.path & " " & Fil.Name
                End If
            Next Fil
    
            Call RecursiveFolder3(SubFld, FileCount, NewSht)
        Next SubFld
    End Sub
    I'll try taking them out and see what effect it has.

    Thanks again for your input here xlnitwit, much appreciated.

  14. #14
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Working with Paths over 255 characters - file count

    They don't seem to be used for anything in what you posted.

  15. #15
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Working with Paths over 255 characters - file count

    Yep I've removed them form everything as I can't see the purpose. I am however now getting another error I'd not encountered before in trying to obtain the 'Last Saved By' attribute or 'owner.'

    I don't know if this is a problem with the unc file path - it wasn't happening before - but I can see the file is missing the attribute but I can't seem to skip it if nothing there, not even using On Error Resume next. The Error is 70 - Permission Denied. Here's the code:

    On Error Resume Next
    LastSavedBy = ""
    Set secUtil = CreateObject("ADsSecurityUtility")
    Set secDesc = secUtil.GetSecurityDescriptor(oFolder.path & "\" & Fil.Name, 1, 1)
    LastSavedBy = secDesc.owner
    On Error GoTo 0
    And the error is occurring on this line:
    Set secDesc = secUtil.GetSecurityDescriptor(oFolder.path & "\" & Fil.Name, 1, 1)
    Feel like I'm chasing my tail with this!

  16. #16
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Working with Paths over 255 characters - file count

    I have been testing this over the last couple of days and the error has not re-occurred. Therefore I'm marking this as Solved. Thanks for your help, very much appreciated.

  17. #17
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Working with Paths over 255 characters - file count

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. About file paths
    By Moragtao in forum Excel General
    Replies: 1
    Last Post: 07-19-2017, 05:10 AM
  2. Linked File Paths Change When File is Moved
    By belewfripp in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-06-2013, 12:18 PM
  3. [SOLVED] Dynamic file paths. HELP!
    By christhweatt in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-07-2013, 04:22 PM
  4. Export To Inf file & Zip the Files from the given file paths picking every 10 rows
    By vivekhalder in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-18-2012, 03:59 AM
  5. Replies: 3
    Last Post: 04-29-2009, 03:59 AM
  6. [SOLVED] file paths
    By hr in forum Excel General
    Replies: 0
    Last Post: 12-07-2005, 01:10 PM
  7. Relative file paths
    By Jim in forum Excel General
    Replies: 7
    Last Post: 05-24-2005, 07:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1