Results 1 to 4 of 4

Thread: VBA to fetch folder paths only

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    23

    VBA to fetch folder paths only

    Hi Friends,
    I have a code, which i got from online, the purpose of code is to fetch all the files names & its paths with hyperlink, now i need the code to be edited for fetching only folder names & its folder paths with hyperlink.
    Could anyone please help me on this....Thanks in Advance.
    Code:
    Public fPath As String
    Public IsSubFolder As Boolean
    Public iRow As Long
    Public fso As Scripting.FileSystemObject
    Public SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
    Public FileItem As Scripting.File
    Public IsFileTypeExists As Boolean
    
    Public Sub ListFilesInFolder(SourceFolder As Scripting.Folder, IncludeSubfolders As Boolean)
    
        On Error Resume Next
        For Each FileItem In SourceFolder.Files
       
            Cells(iRow, 2).Formula = iRow - 13
            Cells(iRow, 3).Formula = FileItem.Name
            Cells(iRow, 4).Formula = FileItem.Path
            'Cells(iRow, 5).Formula = Int(FileItem.Size / 1024)
            Cells(iRow, 6).Formula = FolderItem.Name
            'Cells(iRow, 6).Formula = FileItem.Type
            Cells(iRow, 7).Formula = FileItem.DateLastModified
            Cells(iRow, 8).Select
            Selection.Hyperlinks.add Anchor:=Selection, Address:= _
            FileItem.Path, TextToDisplay:="Click Here to Open File"
            
    Cells(iRow, 8).Formula = "=HYPERLINK(""" & FileItem.Path & """,""" & "Click Here to Open File" & """)"
                          
            iRow = iRow + 1 ' next row number
            Next FileItem
            
            If IncludeSubfolders Then
                For Each SubFolder In SourceFolder.SubFolders
                    ListFilesInFolder SubFolder, True
                    Next SubFolder
                End If
                
                Set FileItem = Nothing
                Set SourceFolder = Nothing
                Set fso = Nothing
              
            End Sub
          
            Public Sub ListFilesInFolderXtn(SourceFolder As Scripting.Folder, IncludeSubfolders As Boolean)
          
                On Error Resume Next
                Dim FileArray As Variant
                
                FileArray = Get_File_Type_Array
                
                For Each FileItem In SourceFolder.Files
                    
                    Call ReturnFileType(FileItem.Type, FileArray)
                    
                    If IsFileTypeExists = True Then
                        
                        Cells(iRow, 2).Formula = iRow - 13
                        Cells(iRow, 3).Formula = FileItem.Name
                        Cells(iRow, 4).Formula = FileItem.Path
                        'Cells(iRow, 5).Formula = Int(FileItem.Size / 1024)
                        Cells(iRow, 6).Formula = FolderItem.Name
                        'Cells(iRow, 6).Formula = FileItem.Type
                        Cells(iRow, 7).Formula = FileItem.DateLastModified
                        
                        Cells(iRow, 8).Select
                       Selection.Hyperlinks.add Anchor:=Selection, Address:= _
                       FileItem.Path, TextToDisplay:="Click Here to Open File"
                        
    Cells(iRow, 8).Formula = "=HYPERLINK(""" & FileItem.Path & """,""" & "Click Here to Open File" & """)"
                        
                        iRow = iRow + 1 ' next row number
                        
                    End If
                    Next FileItem
                    
                    If IncludeSubfolders Then
                        For Each SubFolder In SourceFolder.SubFolders
                            ListFilesInFolderXtn SubFolder, True
                            Next SubFolder
                        End If
                        
                        Set FileItem = Nothing
                        Set SourceFolder = Nothing
                        Set fso = Nothing
        
                    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA to fetch folder paths only

    from your description i am unsure about what you want to do
    i am sure not all the code you have has been posted
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    23

    Re: VBA to fetch folder paths only

    Hi Westconn,
    Thank you so much for your responce, i have completed it.

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: VBA to fetch folder paths only

    One of the reasons i dislike the FSO: The Syntax just doesn't make sense to me.
    There is a thread here in Office-Dev with something similiar from a few weeks ago.
    It was actually about renaming files and folders, but the mechanism is the same: Collectin everything first.
    Try a search for it.
    I posted a solution without FSO but with API
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Posting Permissions

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



Click Here to Expand Forum to Full Width