AllAPI Network - The KPD-Team

 
Allapi Network
 API-Guide
 ApiViewer

 API List

 
API Resources
 Tips & Tricks
 VB Tutorials
 Error Lookup
 
Misc Stuff
 VB examples
 VB Tools
 VB Links
 Top Downloads
 
This Site
 Search Engine
 Contact Form
 

Donate to AllAPI.net

How to Quickly create entire directory structures

If you ever need to quickly create an entire directory structure for your application, the following examples are a quick and easy way.

There are two methods to create a directory structure. You can use a File System Object, or you can use the CreateDirectory API. We'll explain them both.

The File System Object example:

Just paste this code into a new project, and it will create the directory 'c:\Testing\Hello\How\Are\You\'

Public Sub rMkDir(ByVal mdir As String)
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.GetParentFolderName(mdir) <> "" Then rMkDir FSO.GetParentFolderName(mdir)
On Local Error Resume Next
MkDir mdir
End Sub
Private Sub Form_Load()
rMkDir "c:\Testing\Hello\How\Are\You\"
End Sub

Tip submitted by Eric Donoho

The CreateDirectory Example:

It uses the CreateDirectory API and the Dir() function to quickly create all the directories in the path.

Declarations

Copy the following code into the declarations section of the project.

Private Type SECURITY_ATTRIBUTES

nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

Private Declare Function CreateDirectory Lib "kernel32" _
Alias "CreateDirectoryA" (ByVal lpPathName As String, _
lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long      

Code

Public Sub CreateNewDirectory(NewDirectory As String)
Dim sDirTest As String
Dim SecAttrib As SECURITY_ATTRIBUTES
Dim bSuccess As Boolean
Dim sPath As String
Dim iCounter As Integer
Dim sTempDir As String
iFlag = 0
sPath = NewDirectory

If Right(sPath, Len(sPath)) <> "\" Then
    sPath = sPath & "\"
End If

iCounter = 1

Do Until InStr(iCounter, sPath, "\") = 0
    iCounter = InStr(iCounter, sPath, "\")
    sTempDir = Left(sPath, iCounter)
    sDirTest = Dir(sTempDir)
    iCounter = iCounter + 1
    'create directory
    SecAttrib.lpSecurityDescriptor = &O0
    SecAttrib.bInheritHandle = False
    SecAttrib.nLength = Len(SecAttrib)
    bSuccess = CreateDirectory(sTempDir, SecAttrib)
Loop
End Sub      

Use

You can use the code to create whole directory structures easily and quickly.  Fow example, the following code will create the directory c:\test\directory\vb\tips\

Call CreateNewDirectory("c:\test\directory\vb\tips\")

 

 


Copyright © 1998-2007, The Mentalis.org Team - Privacy statement
Did you find a bug on this page? Tell us!
This site is located at http://allapi.mentalis.org/