Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Networks on November 15, 2004, 08:54 PM

Title: Creating Folders
Post by: Networks on November 15, 2004, 08:54 PM
Any idea how to create and check if a folder exists? Such as in the same application path.
Title: Re: Creating Folders
Post by: Hdx on November 15, 2004, 09:15 PM
O yes FoFo just showed me this nifty api.
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


nifty hua?, And using the DIR checks if that folder alreadsy egzists.
~-~(HDX)~-~
Title: Re: Creating Folders
Post by: MyndFyre on November 15, 2004, 09:34 PM
I *believe* that you are able to use the FileSystemObject (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vaobjfilesystemobject.asp) ActiveX/COM object in VB:


Dim fso As Object
fso = CreateObject("Scripting.FileSystemObject")
fso.CreateFolder(FolderPath)
Dim exists As Boolean
exists = fso.FolderExists(FolderPath)
Title: Re: Creating Folders
Post by: Grok on November 16, 2004, 09:44 AM
Quote from: MyndFyre on November 15, 2004, 09:34 PM
I *believe* that you are able to use the FileSystemObject (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vaobjfilesystemobject.asp) ActiveX/COM object in VB:

You believe correctly.  I would use early binding though.


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


Doing it this way would create any parent folders necessary.
Title: Re: Creating Folders
Post by: Blaze on November 17, 2004, 09:06 PM
If you were trying to open a file in another folder and that folder might not exist, try this


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


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.
Title: Re: Creating Folders
Post by: Grok on November 17, 2004, 09:16 PM
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.