Test if main folder and subfolder exist (1 Viewer)

megatronixs

Registered User.
Local time
Today, 03:06
Joined
Aug 17, 2012
Messages
719
Hi all,

I'm having problems creating a second subfolder in the main folder.
The main folder is used to keep inside the reviews of clients inside.
The code I have now, will only create a main folder if it does not exist and then a subfolder with the BIN nr, type review and date. If the client would have a diferent ones and in diferent years. So if it has first "Prototype Model", the next one could be "Prototype Review". The part of the code that checks if the main folder exist works, but how to let the code create in the main folder the second review subfolder? Please see below code I managed so far:

Code:
Private Sub btn_Create_Folder_Click() 'XXXXXXXXXX working onXXXXXXXXXXXXXX
    Dim path1 As String
    Dim path2 As String
    path1 = "C:\Test_Folder\Medium\" & Me.BIN & "_" & RTrim(Me.LE_Name) & "\"
    path2 = "C:\Test_Folder\High\" & Me.BIN & "_" & RTrim(Me.LE_Name) & "\"
    Debug.Print path1
    Debug.Print path2
 
    If Me.Rating = "High" Then
        If Len(Dir(path2, vbDirectory)) > 0 Then
            MsgBox "Folder already exists", vbCritical
            GoTo Exit1
        Else
        MkDir path2
        MkDir path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.START_date, "mmm yyyy") & "\"
        MkDir path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.START_date, "mmm yyyy") & "\" & "All Other Documents\"
        MkDir path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.START_date, "mmm yyyy") & "\" & "Admin\"
        MkDir path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.START_date, "mmm yyyy") & "\" & "Emails\"
        MsgBox "Folder created successfully!", vbInformation
 
        End If
    End If
End Sub

Any ide how to have the code test both of them?

Greetings.
 

namliam

The Mailman - AWF VIP
Local time
Today, 03:06
Joined
Aug 11, 2003
Messages
11,696
I use this to create a folder tree:
Code:
Function CreateFolderTree(MyPath As String)
    Dim c As Integer
    On Error Resume Next
    If InStr(4, MyPath, "\") = 0 Then
        MkDir (MyPath)
    Else
        If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
        For c = 4 To Len(MyPath)
            If Mid(MyPath, c, 1) = "\" Then
                 MkDir Left(MyPath, c)
            End If
        Next c
    End If
End Function
 

megatronixs

Registered User.
Local time
Today, 03:06
Joined
Aug 17, 2012
Messages
719
Hi,

How can I use the above code?
Do I need to call the function some how?
It is kind of next level for me and will not be able to implent it :-(

Greetings.
 

namliam

The Mailman - AWF VIP
Local time
Today, 03:06
Joined
Aug 11, 2003
Messages
11,696
It isnt as complex as it looks....

simply call it like so:
Code:
CreateFolderTree "C:\Temp\Test\Namliam\Megatronixs\Some space folder\2015\01"

Should create the entire folder strucuture without fail
 

megatronixs

Registered User.
Local time
Today, 03:06
Joined
Aug 17, 2012
Messages
719
HI,

I did that and it creates the main folder. How to create the subfolders now and have them checked if they don't exist to make them?

Greetings.
 

namliam

The Mailman - AWF VIP
Local time
Today, 03:06
Joined
Aug 11, 2003
Messages
11,696
Dont really need to check, the MKdir does it for you basicaly...

The entire folder structure should be created, not just the main folder.
 

megatronixs

Registered User.
Local time
Today, 03:06
Joined
Aug 17, 2012
Messages
719
Hi,

When a user clicks the button to create the same folder, it should give error and not create the folder, it could be that everything will be overwritten :-(

The top code creates the subfolders, but when I have diferent type review, it should be able to make that folder and subfolders, now it just tells me that the main folder exist and don't create the new one. Any chance this could be done?

Greetings.
 

namliam

The Mailman - AWF VIP
Local time
Today, 03:06
Joined
Aug 11, 2003
Messages
11,696
a file can be overwritten, a folder cant.... well it wont remove and create it and/or replace files

If you feel you want to check the folders first.... make a function like so
Code:
Function CheckBeforeCreateFolder(MyPath As String)
    If Len(Dir(path2, vbDirectory)) > 0 Then
        MsgBox "Folder already exists", vbCritical
    Else
        MkDir MyPath
    End If
End Function

And change your code to:
Code:
    If Me.Rating = "High" Then
        CheckBeforeCreateFolder path2
        CheckBeforeCreateFolder path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.START_date, "mmm yyyy") & "\"
        CheckBeforeCreateFolder path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.START_date, "mmm yyyy") & "\" & "All Other Documents\"
        CheckBeforeCreateFolder path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.START_date, "mmm yyyy") & "\" & "Admin\"
        CheckBeforeCreateFolder path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.START_date, "mmm yyyy") & "\" & "Emails\"
    End If

If you want to fail out on any one folder existing try something like:
Code:
    Dim path1 As String
    Dim path2 As String
    Dim myError As String
    Dim TempPath1 As String
    Dim TempPath2 As String
    Dim TempPath3 As String
    Dim TempPath4 As String
    Dim TempPath5 As String
    
    path1 = "C:\Test_Folder\Medium\" & Me.BIN & "_" & RTrim(Me.LE_Name) & "\"
    path2 = "C:\Test_Folder\High\" & Me.BIN & "_" & RTrim(Me.LE_Name) & "\"
    Debug.Print path1
    Debug.Print path2
    
    TempPath1 = path2
    TempPath2 = path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.START_date, "mmm yyyy") & "\"
    TempPath3 = path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.START_date, "mmm yyyy") & "\" & "All Other Documents\"
    TempPath4 = path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.START_date, "mmm yyyy") & "\" & "Admin\"
    TempPath5 = path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.START_date, "mmm yyyy") & "\" & "Emails\"
    If Len(Dir(TempPath1, vbDirectory)) > 0 Then myError = TempPath1 & vbNewLine
    If Len(Dir(TempPath2, vbDirectory)) > 0 Then myError = TempPath2 & vbNewLine
    If Len(Dir(TempPath3, vbDirectory)) > 0 Then myError = TempPath3 & vbNewLine
    If Len(Dir(TempPath4, vbDirectory)) > 0 Then myError = TempPath4 & vbNewLine
    If Len(Dir(TempPath5, vbDirectory)) > 0 Then myError = TempPath5 & vbNewLine
    If Len(myError) > 0 Then
        MsgBox "Unable to create (all) folders: " & vbNewLine & myError
    Else
        MkDir TempPath1
        MkDir TempPath2
        MkDir TempPath3
        MkDir TempPath4
        MkDir TempPath5
        MsgBox "Folder created successfully!", vbInformation
    End If

Just FYI, in folder names, it is more usual to have a format of YYYY MM so you keep the order of the months. Otherwize you get a jumble that will put Apr and Aug as your first two months
 

megatronixs

Registered User.
Local time
Today, 03:06
Joined
Aug 17, 2012
Messages
719
Hi,

I got nothing to work :-(
I changed everything, tested both, but no luck.
No possible change in the first code to continue with the creation if the main folder exist anyway?

Greetings.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 02:06
Joined
Sep 12, 2006
Messages
15,614
I forget the exact call, but I am pretty sure there is an API call that creates the final folder, and also any folders needed on the way.

here you go, the cunningly titled

makesuredirectorypathexists

https://msdn.microsoft.com/en-gb/library/windows/desktop/ms680352(v=vs.85).aspx

the above was c++. The following is the vba declaration, taken from here

http://www.devhut.net/2011/09/15/vba-create-directory-structurecreate-multiple-directories/

Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long
 

namliam

The Mailman - AWF VIP
Local time
Today, 03:06
Joined
Aug 11, 2003
Messages
11,696
Hi,

I got nothing to work :-(
I changed everything, tested both, but no luck.
Sorry dont know what you are doing wrong ?

No possible change in the first code to continue with the creation if the main folder exist anyway?
What stops your code? Simply remove the "stop" and you should be golden?
 

megatronixs

Registered User.
Local time
Today, 03:06
Joined
Aug 17, 2012
Messages
719
Hi,
I will recreate the parts of the database today at home (I can't upload nothing from work)
and then you can have a look at what I try to do and will be easier to understand.

Greetings.
 

megatronixs

Registered User.
Local time
Today, 03:06
Joined
Aug 17, 2012
Messages
719
Hi,

Find attached the test database. The first main folder creates Ok and the first subfolder. If the type review would be changed to something else and the date, I would like to create that subfolder with his subfolders in the main folder and only get error message when I try to create the exact same subfolder with subfolders.

Greetings.
 

Attachments

  • Type_Review.mdb
    312 KB · Views: 190

megatronixs

Registered User.
Local time
Today, 03:06
Joined
Aug 17, 2012
Messages
719
Hi Namliam,

Did you had a chance to check the database from previous post?

Greetings.
 

namliam

The Mailman - AWF VIP
Local time
Today, 03:06
Joined
Aug 11, 2003
Messages
11,696
My code from above.... slightly changed to work with the new control names...
And a small fix to display the error message more properly....

Seems to work just fine, what about this doesnt work or doesnt suite your needs?
Code:
Private Sub btn_Create_Folder_Click()

    Dim path1 As String
    Dim path2 As String
    Dim myError As String
    Dim TempPath1 As String
    Dim TempPath2 As String
    Dim TempPath3 As String
    Dim TempPath4 As String
    Dim TempPath5 As String
    
    path1 = "C:\Test_Folder\Medium\" & Me.BIN & "_" & RTrim(Me.Client_Name) & "\"
    path2 = "C:\Test_Folder\High\" & Me.BIN & "_" & RTrim(Me.Client_Name) & "\"
    Debug.Print path1
    Debug.Print path2
    Stop
    TempPath1 = path2
    TempPath2 = path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.Start_date, "mmm yyyy") & "\"
    TempPath3 = path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.Start_date, "mmm yyyy") & "\" & "All Other Documents\"
    TempPath4 = path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.Start_date, "mmm yyyy") & "\" & "Admin\"
    TempPath5 = path2 & Me.BIN & "_" & test(Me.Type_Review) & "_" & Format(Me.Start_date, "mmm yyyy") & "\" & "Emails\"
    If Len(Dir(TempPath1, vbDirectory)) > 0 Then myError = myError & TempPath1 & vbNewLine
    If Len(Dir(TempPath2, vbDirectory)) > 0 Then myError = myError & TempPath2 & vbNewLine
    If Len(Dir(TempPath3, vbDirectory)) > 0 Then myError = myError & TempPath3 & vbNewLine
    If Len(Dir(TempPath4, vbDirectory)) > 0 Then myError = myError & TempPath4 & vbNewLine
    If Len(Dir(TempPath5, vbDirectory)) > 0 Then myError = myError & TempPath5 & vbNewLine
    If Len(myError) > 0 Then
        MsgBox "Unable to create (all) folders: " & vbNewLine & myError
    Else
        MkDir TempPath1
        MkDir TempPath2
        MkDir TempPath3
        MkDir TempPath4
        MkDir TempPath5
        MsgBox "Folder created successfully!", vbInformation
    End If
End Sub
 

megatronixs

Registered User.
Local time
Today, 03:06
Joined
Aug 17, 2012
Messages
719
Hi,

Thanks for the code. I see that I missed to add to the code that checks if it is High or Medium for the second path :-(
Any Idea how to implement that in a easier way isted of my old way from the first post?

Greetings and thanks for you help :)
 

namliam

The Mailman - AWF VIP
Local time
Today, 03:06
Joined
Aug 11, 2003
Messages
11,696
Do y ou mean something like:
Code:
    path2 = "C:\Test_Folder\" & Me.Rate & "\" & Me.BIN & "_" & RTrim(Me.Client_Name) & "\"

??

Would almost seem too basic an answer ?
 

megatronixs

Registered User.
Local time
Today, 03:06
Joined
Aug 17, 2012
Messages
719
Hi,
It is about that if a case is High, it needs to be created in the folder High:
Code:
'    path2 = "C:\Test_Folder\High\" & Me.BIN & "_" & RTrim(Me.Client_Name) & "\"
If a case is Medium:
Code:
' path1 = "C:\Test_Folder\Medium\" & Me.BIN & "_" & RTrim(Me.Client_Name) & "\"

in the previous code I had:
Code:
'    If Me.Rate = "High" Then
'        If Len(Dir(path2, vbDirectory)) > 0 Then
'            MsgBox "Folder already exists", vbCritical
'            GoTo Exit1
'        Else

and then for the medium:
Code:
'    If Me.Rate = "Medium" Then
'        If Len(Dir(path1, vbDirectory)) > 0 Then
'            MsgBox "Folder already exists", vbCritical
'            GoTo Exit1
'        Else

I was kind of forgeting this important part :-(

Greetings.
 

namliam

The Mailman - AWF VIP
Local time
Today, 03:06
Joined
Aug 11, 2003
Messages
11,696
and taking the rate from the form wouldnt fix that?
 

megatronixs

Registered User.
Local time
Today, 03:06
Joined
Aug 17, 2012
Messages
719
Hi,

I was wondering how to do that part without using the "If"
(sorry for to many silly questions and tricky code, just try to learn on the job and every one wants the new improvement yesterday already)

Greetings.
 

Users who are viewing this thread

Top Bottom