Results 1 to 14 of 14
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195

    Retrieving Folder SubFolder Names

    Hi guy's hope you are all well

    I have had a look around on older systems i have and tried a few things but no success

    I am trying to retrieve subfolder names within a folder, the following is an example, but access crashes on this code

    Code:
    Dim strPath As String, strFolders As String
    
    strPath = "c:\"
    
    
    strFolders = Dir(strPath, vbDirectory)
    
    
    If Len(strPath & strFolders) > 0 Then
    
    
        While strFolders <> ""
    
    
        If (GetAttr(strPath & strFolders) And vbDirectory) = vbDirectory Then
        
            Debug.Print strFolders
        
        End If
        
        Wend
        
    End If
    I am not sure what If GetAttr does but found a few other people have added this into their folder listing



    Once i get the folder names into Immediate window, i am going to then create a string and add to an input box so a user can select a number to open folder by using Application.FollwHyperlink

    such as

    Code:
    Chr(149) & " " & " 1 " & Chr(149) & " To Open Folder Name 1 & vbcrlf & _
    Chr(149) & " " & " 2 " & Chr(149) & " Tio Open Folder Name 2 etc...
    What is the correct code for achieving this please ?

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Code:
    Sub slistfolders(strFolder, Optional IncludeSubFolders As Boolean = True)
    
    
        '    Dim fso As New FileSystemObject
        '    Dim fol As Folder,  sfol As Folder
    
    
        Dim fso As Object
        Dim fol As Object, sfol As Object
        Set fso = CreateObject("Scripting.filesystemobject")
    
    
        Set fol = fso.GetFolder(strFolder)
    
    
        For Each sfol In fol.SubFolders
    
                     debug.Print  sfol.Path
            
            If IncludeSubFolders = True Then
           
                slistfolders sfol.Path, IncludeSubFolders
            
            End If
            
        Next
    
    
    End Sub
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Thanks Moke123, that worked a treat

  4. #4
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Hi Moke123, not only your suggestion works, i have added
    Code:
    Debug.Print mID(sfol.path, InStrRev(sfol.path, "\") + 1)
    to retrieve just folder names and not full path

    Excellent stuff, thanks again

  5. #5
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Hi Guy's, I thought I could clear this but after a slight adjustment to Moke123 suggestion

    How can i add an array of numbers for each folder and add to an input box so a user can select a number to open the folder name ?

    Immediate window displays folder names correctly

    Code:
    Dim fol As Object, sfol As Object, fso As Object 
        Set fso = CreateObject("Scripting.filesystemobject")
        
        strFolder = "T:\Dave\Images\Stored Images\"
        
        Set fol = fso.GetFolder(strFolder)
    
    
        For Each sfol In fol.SubFolders
    
    
            Debug.Print mID(sfol.path, InStrRev(sfol.path, "\") + 1)
            
        Next
    How do i add something like

    Code:
    Dim iFolders as Integer, i as Integer
    
    For i = 1 To mID(sfol.path, InStrRev(sfol.path, "\") + 1)
    
    iFolders = InputBox("Enter Which Folder To Open ?" & vbcrlf & vbcrlf & _
    Chr(149) & " " & i & " " Chr(149) & " " & Add Array Of Folder Names)
    Hope i have asked this correctly to understand

    Then i Can add something like

    Code:
    Select Case i
    
    case = 1
    
    Application.FollowHyperlink strFolder & FolderName
    
    Etc.......

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Why would you want to use numbers?
    I would give them a combo on a form, but show the full path, as you might have two names the same, but in different folders.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Hello WGM, i have this now I think, was thinking of using listIndex so a user can select a number to open a folder, added to a listbox

    Totally agree about combo but this is going to be integrated into a current procedure using a Option Control to save images to a selected folder but totally agree if this was a new procedure

    Code:
    Dim fs, sf, fl As Variant    Dim strInput As String
        
        Me.lstFolders.RowSource = "Value List"
        Me.lstFolders.RowSource = ""
        
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set fl = fs.GetFolder(strFolder)
        Set sf = fl.SubFolders
        
        For Each fl In sf
            Me.lstFolders.AddItem fl.Name
        Next
            Me.lstFolders.Requery
        
        For i = 0 To Me.lstFolders.ListCount - 1
            strList = strList & Me.lstFolders.ItemData(i) & vbCrLf
        Next
            strInput = InputBox("Enter Folder To Open ?" & vbCrLf & vbCrLf & _
                Chr(149) & " " & i & " " & Chr(149) & " " & strList, "ENTER NUMBER")
    I have this very close but inputbox displaying both folder names but only 1 number

    List box is set to no headers, is this to do with the 1st row being 0 (zero) ?

  8. #8
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    So So Close with this, just can't open the correct folder, it's opening strfolder no matter which list index number is selected!!!

    Code:
    Dim fs, sf, fl As Variant    Dim iInput As Integer
        Dim strFolder As String
        
        Me.lstFolders.RowSource = "Value List"
        Me.lstFolders.RowSource = ""
        
        strFolder = "T:\DMT Ltd\Images\Stored Images\"
        
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set fl = fs.GetFolder(strFolder)
        Set sf = fl.SubFolders
        
        For Each fl In sf
            Me.lstFolders.AddItem fl.Name
        Next
            Me.lstFolders.Requery
        
        For i = 0 To Me.lstFolders.ListCount - 1
    
    
            strList = strList & Chr(149) & " " & i & " " & Chr(149) & " " & Me.lstFolders.ItemData(i) & vbCrLf & vbCrLf
            
        Next i
        
            iInput = InputBox("Enter Folder To Open ?" & vbCrLf & vbCrLf & _
                strList, "ENTER NUMBER")
            
            Application.FollowHyperlink strFolder & Me.lstFolders.ItemData(i) & "\"

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    That is because you only append the number when you show the list, instead of appending for each folder.
    Input boxes are hard to validate as well.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    ahh so if i am understanding you correctly, my For i line needs to be inside the next i ?

    all is displaying correctly just always opening main folder and not subfolder

    or if i am reading the sequence correct

    instead of:
    For i = 0 To Me.lstFolders.ListCount - 1
    do this
    for i = 0 to fl.Name

  11. #11
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    to retrieve just folder names and not full path
    To just get the folder name use sfol.name

    in the example double click the listbox to open folder.
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  12. #12
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    In your code in post #8 your using late binding. Add a reference to MS Scripting runtime and the set the fs

    Code:
    Set fs = new FileSystemObject.
    Then you'll have intellisense to help you out.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  13. #13
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    here's one way to do it using an inputbox
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  14. #14
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Hi Moke123, will check to see what you have done thank you

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Displaying Folder Names Inputbox
    By DMT Dave in forum Access
    Replies: 4
    Last Post: 04-15-2021, 04:34 AM
  2. Create Folder based on field names
    By dinsey90 in forum Access
    Replies: 12
    Last Post: 10-04-2017, 03:38 PM
  3. Trying to send a report to a subfolder
    By Gina Maylone in forum Programming
    Replies: 2
    Last Post: 12-04-2014, 05:18 AM
  4. combobox based on folder names
    By sdel_nevo in forum Programming
    Replies: 3
    Last Post: 11-18-2014, 12:40 PM
  5. count number of file names within a folder containing
    By dumbledown in forum Programming
    Replies: 2
    Last Post: 04-24-2012, 02:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums