+ Reply to Thread
Results 1 to 7 of 7

Is there a way to get Specific subfolder level names and put in a column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-15-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2016 / Office 365
    Posts
    115

    Is there a way to get Specific subfolder level names and put in a column

    I am using the following code to maintain a list of files in a specific server directory. We use this list as a Table of Contents and the sub folder names are based on Manufacture and then Type. We now have some additional classifications that we want to use under each type that can create additional subfolders. If those additional subfolders get created, the columns on the TOC that match to the Type and the Manufacture will no longer work as coded. Is there a way to program it to always get the 6th and 7th folder from the File Path and put them in the columns that they are associated with so that if the Type subfolder ends up with additional subfolders, the TOC with always display the Manufacture and Type for that particular file? See Lines 17, 18, 20-23 of attached file. The folders listed are not the Manufacture and Type, They are Type and Sub Type.
    Sub RecursiveFolder(objFolder As Scripting.Folder, IncludeSubFolders As Boolean)
    
         'Declare the variables
         Dim objFile As Scripting.File
         Dim objSubFolder As Scripting.Folder
         Dim NextRow As Long, lr As Long, Filename As Range
         Dim ws1 As Worksheet, d As Date, NewFile As Range
         
         Set ws1 = ThisWorkbook.Sheets("Sheet1")
         lr = ws1.Range("B" & Rows.Count).End(xlUp).Row
         Set Filename = ws1.Range("B2:B" & lr)
         
         'Find the next available row
         NextRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
         
         'Loop through each file in the folder
        For Each objFile In objFolder.Files
         'Look for New Files
            With ws1.Range("B2:B" & lr)
                    Set NewFile = .Find(What:=objFile.Name, _
                    After:=.Cells(.Cells.Count), _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False)
                If NewFile Is Nothing Then
                        ws1.Cells(NextRow, "B").Value = objFile.Name
                            ws1.Cells(NextRow, "B").Select
                            ws1.Hyperlinks.Add Anchor:=Selection, Address:=(objFile.Path), TextToDisplay:=Selection.Text
                        ws1.Cells(NextRow, "C").Value = objFile.DateCreated
                        ws1.Cells(NextRow, "D").Value = objFolder.Name
                        ws1.Cells(NextRow, "E").Value = objFolder.ParentFolder.Name
                        
                        NextRow = NextRow + 1
                End If
            End With
        Next objFile
    
         'Loop through files in the subfolders
         If IncludeSubFolders Then
             For Each objSubFolder In objFolder.SubFolders
                 Call RecursiveFolder(objSubFolder, True)
             Next objSubFolder
         End If
         
        Call Sort
         
    End Sub

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,906

    Re: Is there a way to get Specific subfolder level names and put in a column

    Yes if you know for sure that it's always the 6th and the 7th you want you can path the fullfolder path to a variable and addres it as an array where the delimiter is the backslash and just take out the ones you want.
    Something like this:

    Sub Test123()
    Dim foldString As String
    Dim foldArr  As Variant
    foldString = "S:\Server\SubFolder1\SubFolder2\SubFolder3\SubFolder4\SubFolder5\SubFolder6\SubFolder7\SubFolder8\SubFolder10\"
    foldArr = (Split(foldString, "\"))
    Debug.Print foldArr(LBound(foldArr) + 6), foldArr(LBound(foldArr) + 7)
    End Sub
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Forum Contributor
    Join Date
    04-15-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2016 / Office 365
    Posts
    115

    Re: Is there a way to get Specific subfolder level names and put in a column

    Thanks Keebellah, that looks like it did the trick.

  4. #4
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,906

    Re: Is there a way to get Specific subfolder level names and put in a column

    Good to hear.
    Happy oding, don't forget to mark the post SOLVED

  5. #5
    Forum Contributor
    Join Date
    04-15-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2016 / Office 365
    Posts
    115

    Re: Is there a way to get Specific subfolder level names and put in a column

    One more thing, as I was getting ready to mark this solved they asked for an additional column. Each file name begins with a part number that is followed by a -. Would I be able to use another array to parse out the part number between "\" and "-"?

    Path example \\File Server\Files\Main Folder\Department\Library\Mechanical\Manufacture\Type\012345 - Part Description.dwg

  6. #6
    Forum Contributor
    Join Date
    04-15-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2016 / Office 365
    Posts
    115

    Re: Is there a way to get Specific subfolder level names and put in a column

    Keebellah,
    I figured out my add on question using your split method, but this time I just split the File Name only so I did not have to worry about multiple parameters. Thanks a lot for the help on the split function I had not used that before.

    Final code is below

    Sub RecursiveFolder(objFolder As Scripting.Folder, IncludeSubFolders As Boolean)
    
         'Declare the variables
         Dim objFile As Scripting.File
         Dim objSubFolder As Scripting.Folder
         Dim NextRow As Long, lr As Long, Filename As Range
         Dim ws1 As Worksheet, NewFile As Range
         Dim Foldstring As String, Namestring As String, foldArr As Variant, nameArr As Variant
         
         Set ws1 = ThisWorkbook.Sheets("Sheet1")
         lr = ws1.Range("B" & Rows.Count).End(xlUp).Row
         Set Filename = ws1.Range("B2:B" & lr)
         
         'Find the next available row
         NextRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
         
         'Loop through each file in the folder
        For Each objFile In objFolder.Files
            With ws1.Range("B2:B" & lr)
                    Set NewFile = .Find(What:=objFile.Name, _
                    After:=.Cells(.Cells.Count), _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False)
                If NewFile Is Nothing Then
                    Namestring = objFile.Name
                    nameArr = (Split(Namestring, " - "))
                        ws1.Cells(NextRow, "E").Value = nameArr(LBound(nameArr))
                        ws1.Cells(NextRow, "B").Value = objFile.Name
                            ws1.Cells(NextRow, "B").Select
                            ws1.Hyperlinks.Add Anchor:=Selection, Address:=(objFile.Path), TextToDisplay:=Selection.Text
                        Foldstring = objFile.Path
                        foldArr = (Split(Foldstring, "\"))
                            ws1.Cells(NextRow, "C").Value = foldArr(LBound(foldArr) + 8)    'objFolder.Name
                            ws1.Cells(NextRow, "D").Value = foldArr(LBound(foldArr) + 7)    'objFolder.ParentFolder.Name
                        
                        NextRow = NextRow + 1
                End If
            End With
        Next objFile
    
         'Loop through files in the subfolders
         If IncludeSubFolders Then
             For Each objSubFolder In objFolder.SubFolders
                 Call RecursiveFolder(objSubFolder, True)
             Next objSubFolder
         End If
         
        Call Sort
         
    End Sub

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,906

    Re: Is there a way to get Specific subfolder level names and put in a column

    Glad that I have been able to help you and give you new ideas

+ 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. Replies: 14
    Last Post: 09-18-2016, 08:13 AM
  2. VBA Loop Through a certain level of subfolder
    By niuyuer in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-24-2016, 11:55 AM
  3. Replies: 0
    Last Post: 09-18-2015, 03:20 AM
  4. Create new folder and subfolder and save file with the names from cell values
    By eccordeiro in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-16-2015, 10:03 AM
  5. Macro to rename photos in a main folder's subfolders to subfolder names
    By Spardante in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-22-2014, 11:41 PM
  6. VBA code needed to move from Outlook 2010 subfolder to Symantec Vault subfolder
    By Marijke in forum Outlook Programming / VBA / Macros
    Replies: 1
    Last Post: 01-09-2014, 12:14 PM
  7. Macro to export folder and subfolder names in Excel?
    By Mustang in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-02-2010, 09:13 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