Results 1 to 7 of 7

Thread: Check for directories

  1. #1
    Guest
    In my program, I have to check if a particular directory exists or not. If not, it would have to be created.
    Example c:\abc\123
    I would have to first check if c:\abc exists. If not create c:\abc and then create c:\abc\123.
    Does anybody have a piece of code which would do this?

    Thanks.

  2. #2
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    Try this ...

    Code:
    Dim strPath As String
    Dim fso As FileSystemObject
      
      strPath = "c:\abc"
      
      ' create file system object
      Set fso = New FileSystemObject
      
      ' Create folder if it does not exist
      If Not fso.FolderExists(strPath) Then
        fso.CreateFolder strPath
        fso.CreateFolder strPath & "\123"
      End If
      
      Set fso = Nothing
    Only works for VB6 though.

    Hope it helps

  3. #3
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    ...or try this:

    Code:
        On Error Resume Next
        
        If Right$(stDirectory, 1) <> "\" Then
            strDirName = stDirectory & "\"
        End If
        
        stFile = Dir$(stDirectory & "*.*", vbDirectory)
        DirExists = Not (stFile = vbNullString)
        
        Err = 0

  4. #4
    Guest
    This is the code I got from this site. This makes sure that
    the entire path is created.

    If Len(Dir$(strDirectory, vbDirectory)) <= 0 Then
    Dim Fpath As String
    Dim Branches() As String
    Dim b As Integer
    Branches = Split(strDirectory, "\")
    For b = LBound(Branches) To UBound(Branches)
    Fpath = Fpath & Branches(b) & "\"
    If Len(Dir$(Fpath, vbDirectory)) <= 0 Then
    MkDir Fpath
    End If
    Next b
    End If

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'Check if directory exists
    '-----------------------------------
    Public Function DirExists(ByVal sDirName As String) As Boolean
    
        Dim sDir As String
    
        On Error Resume Next
    
        DirExists = False
    
        sDir = Dir$(sDirName, vbDirectory)
        If (Len(sDir) > 0) And (Err = 0) Then
            DirExists = True
        End If
    
    End Function
    
    
    
    Private Sub Command1_Click()
        Dim x As Boolean
        x = False
        
        x = DirExists("Directory in question path!")
        
        If DirExists = False Then MkDir "C:\mynewdir"
        Else
        'open your file and do whatever...etc.
        End If
        
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6
    Guest
    Thanks for all your replies.

    As I mentioned earlier, apart from checking and creating the directory in question, I need to make sure that the parent folder exists.(This needs to be done recursively until the drive is found and if not create the parent folder too.
    The code I got from one of the previous posts does just that part.

  7. #7
    New Member
    Join Date
    Jul 2000
    Posts
    1
    Try this ...

    -------------------------
    Dim MyPath As String

    MyPath = "c:\whatever\dude"
    If Dir(MyPath, vbDirectory) = "" Then
    MakeFullDir (MyPath)
    End If
    -------------------------
    Function MakeFullDir(d As String) As Boolean
    Dim I As Integer
    Dim dc As New Collection
    Dim dname As String
    On Error goto ErrHdler
    Do While Dir(d, vbDirectory) = ""
    dname = DeepestDir(d)
    dc.Add dname
    d = Left$(d, Len(d) - Len(dname))
    Loop
    For I = dc.Count To 1 Step -1
    d = d & dc(I) & "\"
    MkDir d
    Next
    Set dc = Nothing
    MakeFullDir = True
    Exit Function
    ErrHdler:
    Set dc = Nothing
    MakeFullDir = False
    End Function
    -------------------------
    Function DeepestDir(d As String) As String
    If Right$(d, 1) = "/" Or Right$(d, 1) = "\" Then d = Left$(d, Len(d) - 1)
    DeepestDir = GetFileNamefromFullPath(d)
    End Function
    -------------------------
    Function GetFileNamefromFullPath(fp As String) As String
    Dim l As Integer
    l = Len(fp)
    If l > 0 Then
    Do While l > 1
    If (Mid$(fp, l, 1) = "/" Or Mid$(fp, l, 1) = "\") Then Exit Do
    l = l - 1
    Loop
    End If
    If l > 0 Then
    GetFileNamefromFullPath = Mid$(fp, l + 1)
    Else
    GetFileNamefromFullPath = fp
    End If
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width