+ Reply to Thread
Results 1 to 7 of 7

Check if Folder exists

Hybrid View

  1. #1
    Registered User
    Join Date
    07-09-2010
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2007
    Posts
    32

    Check if Folder exists

    Hello,

    I am trying to check if a folder exists in a directory. I found some great code that does this if you already have the full path. I would like to check if a folder exists who's name contains specific numbers. Is there anyway to modify this existing code to check if a folder exists that contains a specific number? Instead of listing the full path in column A I wanted to list the specific numbers I am looking for. Once found and if possible I also need the full folder name printed on the next adjacent column (c). Any help would be great!

    Public Function FileFolderExists(strFullPath As String) As Boolean
    'Author       : Ken Puls (www.excelguru.ca)
    'Macro Purpose: Check if a file or folder exists
        
        If Not Dir(strFullPath, vbDirectory) = vbNullString Then
            FileFolderExists = True
        Else
            FileFolderExists = False
        End If
        
    End Function
    Sub Macro1()
           
        For Each Cell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
            If IsEmpty(Cell) = True Then
                Cell.Offset(0, 1).Value = "No suggested directory has been entered into " & Cell.Address(False, False)
            ElseIf FileFolderExists(Cell.Value) = True Then
                Cell.Offset(0, 1).Value = "Folder Exists"
            Else
                Cell.Offset(0, 1).Value = "Folder Does Not Exist"
            End If
        Next Cell
        
    End Sub

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Check if Folder exists

    Hi akq125,

    You need to concatenate the cell value as part of the directory path to be checked. For example, if I had 10, 11 and 12 in cells A2, A3 and A4 and I wanted to check if directories "Office10" or "Office11" or "Office12" existed in the "C:\Program Files\Microsoft Office" path, I'd use the following:

    Option Explicit
    Public Function FileFolderExists(strFullPath As String) As Boolean
    'Author       : Ken Puls (www.excelguru.ca)
    'Macro Purpose: Check if a file or folder exists
        
        If Not Dir(strFullPath, vbDirectory) = vbNullString Then
            FileFolderExists = True
        Else
            FileFolderExists = False
        End If
        
    End Function
    Sub Macro1()
    
        Dim rngCell As Range
           
        For Each rngCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
            If IsEmpty(rngCell) = True Then
                rngCell.Offset(0, 1).Value = "No suggested directory has been entered into " & rngCell.Address(False, False)
            ElseIf FileFolderExists("C:\Program Files\Microsoft Office\Office" & rngCell.Value) = True Then
                rngCell.Offset(0, 1).Value = "Folder Exists"
            Else
                rngCell.Offset(0, 1).Value = "Folder Does Not Exist"
            End If
        Next rngCell
        
    End Sub
    Note how I've also used Option Explicit which has forced me to declare my variable, which I've purposely steered away from an Excel reserved word (i.e. Cell).

    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    07-09-2010
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Check if Folder exists

    Hi Trebor,

    Thank you so much for your response. I have a 5 digit number e.g. 12345 that can be anywhere in the folder name. C:\Program Files\Microsoft Office\Office12345-data_for_other_data. Can the instr function be used, really not sure.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Check if Folder exists

    Hello akq125,

    Does each cell in column "A" represent a different parent directory? That is, A2 = "C:\Program Files\Microsoft Office\", A3 = "C:\Documents and Settings\Owner\" etc?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    07-09-2010
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Check if Folder exists

    Hi Leith,

    The root directory would always be the same e.g. "C:\Program Files\Microsoft Office\", the values in A2 and down would be a list of 5 digit numbers e.g. 45612 that is present in the rest of the folder name. This could be C:\Program Files\Microsoft Office\Data_for_Project_(45612). Sometimes the number is in front of Data but the numbers are always unique.
    I'm trying to get the macro to check if a folder exists with 45612 in the folder name under the root directory C:\Program Files\Microsoft Office\. If it does exist then print the full folder name in the adjacent cell to the right.
    I was hoping that if the macro could tell me the folder exists then it could also return the full folder name. I hope this makes what I'm trying to do a bit more clear. The end result would give me a list of folder directories that i can use to manipulate excel files contained within. Thanks again for your help!

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Check if Folder exists

    Hello akq125,

    Thanks for answering my questions. The Dir function in VBA supports the use of wildcard characters "*", to match one or characters, and "?" to match any single character. Given the layout, it will be easy to adjust your code to find any folder with the number given in column "A".

    Here is the adjusted code...
    
    Sub Macro1A()
           
        Dim Cell As Range
        
            For Each Cell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
                If Dir("C:\Program Files\Microsoft Office\" & "*" & Cell.Text & "*", vbDirectory) Then
                    Cell.Offset(0, 1).Value = "Folder Does Not Exist"
                Else
                    Cell.Offset(0, 1).Value = "Folder Exists"
                End If
            Next Cell
        
    End Sub

  7. #7
    Registered User
    Join Date
    07-09-2010
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Check if Folder exists

    Hi Leith,

    The layout looks good but I was getting a type mismatch runtime error on the line below.

    If Dir("C:\Program Files\Microsoft Office\" & "*" & Cell.Text & "*", vbDirectory) Then
    Any Ideas?

    Thank You.

+ 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