+ Reply to Thread
Results 1 to 6 of 6

Dir function question

Hybrid View

Blue_Diamond Dir function question 11-08-2012, 06:18 PM
HSV Re: Dir function question 11-08-2012, 07:05 PM
Blue_Diamond Re: Dir function question 11-08-2012, 07:19 PM
shg Re: Dir function question 11-08-2012, 07:12 PM
Chippy Re: Dir function question 11-08-2012, 07:33 PM
Blue_Diamond Re: Dir function question 11-08-2012, 09:46 PM
  1. #1
    Registered User
    Join Date
    10-26-2012
    Location
    Springdale, Arkansas
    MS-Off Ver
    Excel 2007
    Posts
    13

    Dir function question

    I would like to iterate through folders. I'm attempting to do so using the dir function. Here is the code I tried:


    FolderName = Dir(PathName, vbDirectory)                                   
    
    Do While FolderName <> ""
    
            MsgBox("FolderName = " & FolderName)
    
            'Other code will eventually go here
               
            FolderName = Dir()                                              
    
    Loop
    I can't understand why the first two FolderName strings that are displayed with MsbBox are "." and ".." (one period and then two periods) before it even gets to the first folder name.

    Also, it not only returns the desired folder names but also the names of the Excel workbooks that are in the same main folder. How do I get the dir function to only return folder names?
    Novice Programmer

  2. #2
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    865

    Re: Dir function question

    Will this do the trick?
    FolderName = Dir(PathName, vbDirectory)                                   
    
    Do until FolderName = ""
            MsgBox("FolderName = " & FolderName)
    
            'Other code will eventually go here
               
            FolderName = Dir()                                              
    
    Loop
    Harry.

  3. #3
    Registered User
    Join Date
    10-26-2012
    Location
    Springdale, Arkansas
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Dir function question

    No, that didn't change anything.

  4. #4
    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: Dir function question

    >>Deleted.

  5. #5
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Dir function question

    "." and ".." are standard DOS folder 'shortcuts'. Type in DIR in a command window and you will see them. "." is the current directory and ".." is the parent directory. In VBA, use an If statement to ignore them and GetAttr to check if the file returned by Dir is a directory:

    Sub List_Subfolders()
    
        Dim folder As String
        Dim fileName As String
        
        folder = "C:\Windows\"
        If Right(folder, 1) <> "\" Then folder = folder & "\"
        
        fileName = Dir(folder, vbDirectory)
        Do While fileName <> ""
            If fileName <> "." And fileName <> ".." Then
            
                'Make sure it's a directory
                If (GetAttr(folder & fileName) And vbDirectory) = vbDirectory Then
                    MsgBox folder & fileName
                End If
    
            End If
            fileName = Dir
        Loop
    
    End Sub
    Post responsibly. Search for excelforum.com

  6. #6
    Registered User
    Join Date
    10-26-2012
    Location
    Springdale, Arkansas
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Dir function question

    Quote Originally Posted by Chippy View Post
    "." and ".." are standard DOS folder 'shortcuts'. Type in DIR in a command window and you will see them. "." is the current directory and ".." is the parent directory. In VBA, use an If statement to ignore them and GetAttr to check if the file returned by Dir is a directory:
    Thanks. That did the trick.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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