Create subfolders within folders...

Posted on 2002-07-17
Last Modified: 2009-12-16
This might be fairly simple..

I need to check and then create subfolders within a folder.
For instance, the path specified to my application is: C:\MyFolder\MyProjects\MyTestApp

At each level, I need to check if the folder exists, if it does then proceed checking the next level, else create it. i.e
Check if MyFolder exists.
If yes then check if MyProjects Exists
If no then create MyFolder folder
and so on.

I DO NOT wish to use the FileSystemObject mechanism to accomplish this.

I would really appreciate any help on this.

Question by:sunilkini
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 28

Expert Comment

ID: 7160758

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

Public Sub CreateNewDirectory(NewDirectory As String)
    Dim sDirTest As String
    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)

End Sub

Private Sub Command1_Click()
'call to create a new directory
Call CreateNewDirectory("c:\testdir\testing\vb\")
End Sub

Author Comment

ID: 7160785
Thank you for your response. The code works fine.

I have a question though:-
My application will be receiving several files that will written into the same folder. So once the folder is created, I do not think there is a need to CreateNewDirectory everytime a file is written to Path just created. Is there a way a check can be done if the Path exists before creating one?


Author Comment

ID: 7160790
My concern is the folders can be purged anytime during the day, which might need my code to create the directory. But majority of the times only checking if the directory exists should suffice.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 28

Expert Comment

ID: 7160796
use dir to check if it exists

Private Sub Command1_Click()
If Dir("C:\Folder\Projects\MyTest", vbDirectory) <> "" Then
    MsgBox "it exists"
    MsgBox "it don't exist"
    ' Then create directory here
End If
End Sub
LVL 28

Accepted Solution

vinnyd79 earned 100 total points
ID: 7160811
using your example dir's:

Private Sub Command1_Click()
If Dir("C:\MyFolder\MyProjects\MyTestApp\MyTestFile", vbDirectory) <> "" Then
    Call CreateNewDirectory("C:\MyFolder\MyProjects\MyTestApp\MyTestFile")
End If
End Sub
LVL 32

Expert Comment

ID: 7160821
Just as an alternative, here is a routine that I use to make a nested directory set.  It uses

Public Const FILE_ATTRIBUTE_INVALID   As Long = -1&  ' = &HFFFFFFFF&
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpfilename As String) As Long

Public Function DirExists(sPathName) As Boolean
    Dim attr As Long

    attr = GetFileAttributes(sPathName)

        DirExists = False
        DirExists = ((attr And FILE_ATTRIBUTE_DIRECTORY) > 0)
    End If
End Function

Public Sub MkDirLong(ByVal sPath As String, Optional IsNetworkPath As Boolean = False)
    Dim sBuild As String
    Dim I As Integer
    I = InStr(sPath, ":")
    If I > 0 Then
        sBuild = Left(sPath, 2)
        sPath = Mid(sPath, 3)
        If Not IsNetworkPath Then
            sBuild = "C:"  ' Default to C drive
            sBuild = ""    ' Assume Current Drive if NetworkPath and No Drive Specified
        End If
    End If
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    I = InStr(2, sPath, "\")
    Do While I > 0
        sBuild = sBuild & Left(sPath, I - 1)
        sPath = Mid(sPath, I)
        If Not DirExists(sDrive & sBuild) Then
            MkDir sBuild
        End If
        I = InStr(2, sPath, "\")
End Sub

To use this:
Private Sub MakeStorage()

Dim sFilePath As String

sFilePath = "C:\MyProgram\MyData\20020716\AM\"   ' Sample path
If Not DirExists(sFilePath) Then MkDirLong(sFilePath)

End Sub

Author Comment

ID: 7160828

Fantastic! Works exactly the way I need it.

Much Thanks!

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Access Search and Replace Using VBA 6 105
message box in access 4 55
using web browser with BING 40 148
Dinamic report to Crosstab query 9 59
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question