Valhalla Legends Forums Archive | Visual Basic Programming | Creating Folders

AuthorMessageTime
Networks
Any idea how to create and check if a folder exists? Such as in the same application path.
November 16, 2004, 2:54 AM
HdxBmx27
O yes FoFo just showed me this nifty api.
[code]Public Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" _
        (ByVal lpPathName As String, _
        lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long

Public Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

Public Function MakeFolder(Path As String) As Long
If Right(Path, 1) <> "\" Then Path = Path & "\"
    If Dir(Path) = vbNullString Then
        Dim SS As SECURITY_ATTRIBUTES
        MakeFolder = CreateDirectory(Path, SS)
    Else
        MakeFolder = -1
    End If
End Function[/code]

nifty hua?, And using the DIR checks if that folder alreadsy egzists.
~-~(HDX)~-~
November 16, 2004, 3:15 AM
Myndfyr
I *believe* that you are able to use the FileSystemObject ActiveX/COM object in VB:

[code]
Dim fso As Object
fso = CreateObject("Scripting.FileSystemObject")
fso.CreateFolder(FolderPath)
Dim exists As Boolean
exists = fso.FolderExists(FolderPath)
[/code]
November 16, 2004, 3:34 AM
Grok
[quote author=MyndFyre link=topic=9569.msg89001#msg89001 date=1100576049]
I *believe* that you are able to use the FileSystemObject ActiveX/COM object in VB:
[/quote]

You believe correctly.  I would use early binding though.

[code]
Private fso as Scripting.FileSystemObject

Private Sub MakeFolder(ByVal Path As String)
    If fso.FolderExists(Path) = False Then
        If fso.FolderExists(fso.GetParentFolder(Path)) = False Then
            MakeFolder fso.GetParentFolder(Path)
        End If
        fso.CreateFolder Path
    End If
End Sub
[/code]

Doing it this way would create any parent folders necessary.
November 16, 2004, 3:44 PM
KkBlazekK
If you were trying to open a file in another folder and that folder might not exist, try this

[code]
Public Function MakeAFolder(ByVal FolderName as string)
On Error Resume Next
    Close #1
    Open (App.Path & "\" & FolderName & "\Checking.txt") For Output As #1
    If Err.Number = 76 Then
        MkDir App.Path & "\TheFolder"
    End If
End  Function
[/code]

In this instance you are trying to see if runtime 76 pops up. As much as I don't like On Error Resume Next, this method wouldn't work without it.

btw, I'm suprised that no one used mkdir, its been around a long time.
November 18, 2004, 3:06 AM
Grok
Notice that my function is recursive.  If you are trying to create a folder in a path whose parent folder does not exist, it calls itself with the parent path.  That parent folder would then get created first, exit, and then the subfolder is created.
November 18, 2004, 3:16 AM

Search