• Welcome to Valhalla Legends Archive.
 

Creating Folders

Started by Networks, November 15, 2004, 08:54 PM

Previous topic - Next topic

Networks

Any idea how to create and check if a folder exists? Such as in the same application path.

Hdx

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)~-~

Proud host of the JBLS server www.JBLS.org.
JBLS.org Status:
JBLS/BNLS Server Status

MyndFyre

I *believe* that you are able to use the FileSystemObject ActiveX/COM object in VB:


Dim fso As Object
fso = CreateObject("Scripting.FileSystemObject")
fso.CreateFolder(FolderPath)
Dim exists As Boolean
exists = fso.FolderExists(FolderPath)
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Grok

#3
Quote from: MyndFyre on November 15, 2004, 09:34 PM
I *believe* that you are able to use the FileSystemObject 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.

Blaze

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.
Quote
Mitosis: Haha, Im great arent I!
hismajesty[yL]: No

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.