Solved

Create subfolders within folders...

Posted on 2002-07-17
7
230 Views
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
\MyTestFile

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.

Thanks.
0
Comment
Question by:sunilkini
  • 3
  • 3
7 Comments
 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
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


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

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

Author Comment

by:sunilkini
Comment Utility
vinnyd79,
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?

Thanks!
0
 

Author Comment

by:sunilkini
Comment Utility
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
use dir to check if it exists

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

Accepted Solution

by:
vinnyd79 earned 100 total points
Comment Utility
using your example dir's:

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

Expert Comment

by:bhess1
Comment Utility
Just as an alternative, here is a routine that I use to make a nested directory set.  It uses

Public Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10&
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)

    If (attr = FILE_ATTRIBUTE_INVALID) Then
        DirExists = False
    Else
        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)
    Else
        If Not IsNetworkPath Then
            sBuild = "C:"  ' Default to C drive
        Else
            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, "\")
    Loop
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
0
 

Author Comment

by:sunilkini
Comment Utility
vinnyd79,

Fantastic! Works exactly the way I need it.

Much Thanks!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now