Checking For Whether A Folder Exists In A Directory Results In Unexpected Result

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,634
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am getting a "Path/File access error with the line of code highlighted red below. The procedure is meant to search a drive location to see if a particular folder exists. If it doesn't, to create it. Otherwise, open it in Windows Explorer.
The error I feel is happening because it'ts trying to create a folder that alrerady exists. The value for ffound is reporting "" whether the folder exists or not.

So, as an example ... suppose txt_model equals "Waterloo". The path for which this folder would exist, it it exists, is "O:\IFM\W\"
In the case "Waterloo" doesn't exist (check if fldpath "O:\IFM\W\Waterloo" exists), ffound returns "", and the folder is created.

However, what is happening is ... "Waterloo" already exists in ""O:\IFM\W\", however, ffound returns "" so the code tries to create it ... again ... and fails. What is wrong with my code that ffound is returning unexpected results?

Rich (BB code):
Public Sub open_folder(txt_model As String)
    Dim modelini As String
    Dim ffound ' As String
    Dim response
    Dim fldpath As String
Stop
    If Right(txt_model, 1) = "." Then txt_model = Left(txt_model, (Len(txt_model) - 1))
    modelini = Left(txt_model, 1)
    fldpath = "O:\IFM\" & modelini & "\" & txt_model & "\"
    ffound = Dir(fldpath)
    If ffound = "" Then 'folder not found. Opt to create one.
        On Error Resume Next
        MkDir "O:\IFM\" & modelini 'what happens here if this directory already exists, which it always will?
        On Error GoTo 0
        MkDir fldpath
        MsgBox "Folder for " & txt_model & " created." & Chr(13) & fldpath, vbInformation, "SUCCESS"
    End If
    Shell "explorer.exe /root," & fldpath, vbNormalFocus
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
However, what is happening is ... "Waterloo" already exists in ""O:\IFM\W\", however, ffound returns "" so the code tries to create it ... again ... and fails

You must use the VbDirectory parameter in the Dir function

Try this:

VBA Code:
Public Sub open_folder(txt_model As String)
  Dim modelini As String, fldini As String
  Dim fldMaster As String, fldpath As String
 
  fldMaster = "O:\IFM\"
  If Dir(fldMaster, vbDirectory) = "" Then
    MkDir fldMaster
  End If
 
  If Right(txt_model, 1) = "." Then txt_model = Left(txt_model, (Len(txt_model) - 1))
  modelini = Left(txt_model, 1)
  fldini = fldMaster & modelini & "\"
  If Dir(fldini, vbDirectory) = "" Then
    MkDir fldini
  End If
 
  fldpath = fldini & txt_model & "\"
  If Dir(fldpath, vbDirectory) = "" Then 'folder not found. Opt to create one.
    MkDir fldpath
    MsgBox "Folder for " & txt_model & " created." & Chr(13) & fldpath, vbInformation, "SUCCESS"
    Shell "explorer.exe /root," & fldpath, vbNormalFocus
  End If
End Sub

Happy new year 🥳
 
Upvote 0
Solution
The following code should create the entire folder structure, not just the last folder. It will not return an error if the entire structure already exists.
VBA Code:
Sub Test()
    Call open_folder("Waterloo.")
End Sub


Sub open_folder(txt_model As String)
    Dim modelini As String
    Dim fldpath As String
    
    If Right(txt_model, 1) = "." Then txt_model = Left(txt_model, (Len(txt_model) - 1))
    modelini = Left(txt_model, 1)
    fldpath = "D:\IFM\" & modelini & "\" & txt_model & "\"

    Call MyMkDir(fldpath)

    Shell "explorer.exe /root," & fldpath, vbNormalFocus
End Sub


Sub MyMkDir(sPath As String)
    Dim iStart As Integer
    Dim aDirs As Variant
    Dim sCurDir As String
    Dim i As Integer

    If sPath <> "" Then
        aDirs = Split(sPath, "\")
        If Left(sPath, 2) = "\\" Then
            iStart = 3
        Else
            iStart = 1
        End If

        sCurDir = Left(sPath, InStr(iStart, sPath, "\"))

        For i = iStart To UBound(aDirs)
            sCurDir = sCurDir & aDirs(i) & "\"
            If Dir(sCurDir, vbDirectory) = vbNullString Then
                MkDir sCurDir
            End If
        Next i
    End If
End Sub
Artik
 
Upvote 0
I hope I've correctly understood your needs.

Here is another version to consider. Note the need to edit the code regarding the paths for your computer.

VBA Code:
Option Explicit

Public Sub open_folder()
    Dim txt_model As String
    Dim modelini As String
    Dim ffound As String
    Dim fldpath As String
    Dim fileURL As String

    txt_model = "YourModelName" ' Replace this with your actual model name
    
    If Right(txt_model, 1) = "." Then txt_model = Left(txt_model, (Len(txt_model) - 1))
    modelini = Left(txt_model, 1)
    fldpath = "C:\Users\logit\OneDrive\Desktop\" & modelini & "\" & txt_model & "\"      'Edit path for your machine
    ffound = Dir(fldpath, vbDirectory)
    
    If ffound = "" Then 'folder not found. Opt to create one.
        On Error Resume Next
        
        
        'Edit path for your machine
        ' Create the main directory if it doesn't exist
        
        MkDir "C:\Users\logit\OneDrive\Desktop\" & modelini
        
        
        
        On Error GoTo 0
        MkDir fldpath ' Create the specific model directory
        MsgBox "Folder for " & txt_model & " created." & Chr(13) & fldpath, vbInformation, "SUCCESS"
    End If
    
    ' Convert the folder path to a file URL
    fileURL = "file:///" & Replace(fldpath, "\", "/")
    
    ' Open the folder in the default web browser
    Dim shell As Object
    Set shell = CreateObject("WScript.Shell")
    shell.Run "msedge.exe """ & fileURL & """", 1 ' Using Microsoft Edge as an example
End Sub

Please let me know if this version works for you even if you decide to use another contributor's version. Thanks.
 
Upvote 0
Thank you kind folks for offering up your solutions. A great community here at Mr. Excel.
Dante, your solution worked as expected. Thanbks again. Artik and Logit, in consideration of time I haven't tested your solutions yet. However, I anticipate needing to do this again in my project, so I will try your's at that time as an alternative and report back!
 
Upvote 0

Forum statistics

Threads
1,225,367
Messages
6,184,549
Members
453,242
Latest member
meoo191

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top