Creating folders

KGee

Well-known Member
Joined
Nov 26, 2008
Messages
537
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I want to test for a path on the network to see if it exists, and create the various folders I need if they are missing. Say my folders are called abc, def and ghi. I wanted to check for all for all three and create each as necessary using the code below. Will using the elseif skip the others or will it go through each if regardless?

Code:
If Dir("..file_path\abc\", vbDirectory) = "" Then
    MkDir ("..file_path\abc\")
ElseIf Dir("..file_path\abc\def\", vbDirectory) = "" Then
    MkDir ("..file_path\abc\def\")
ElseIf Dir("..file_path\abc\def\ghi\", vbDirectory) = "" Then
    MkDir ("..file_path\abc\def\ghi\") 
End If

Is there an easier way to do this or can I just check for "ghi" and have all three created in the process if they are not present?
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
If that is all you need to check to ensure their existence, I don't see why not...


<font face=Courier New><SPAN style="color:#00007F">If</SPAN> Dir("..file_path\abc\def\ghi\", vbDirectory) = "" <SPAN style="color:#00007F">Then</SPAN><br>    MkDir ("..file_path\abc\")<br>    MkDir ("..file_path\abc\def\")<br>    MkDir ("..file_path\abc\def\ghi\")<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN></FONT>
 
Upvote 0
I suppose you may just determine what happens if you create a directory that already exists - as long as it doesn't overwrite, then simplest would be to just attempt to create each one:

On Error Resume Next
mkdir abc
mkdir abc/def
mkdir abc/def/ghi
On Error Goto 0

Your code as written would fail because if abc doesn't exist, it would create it. But then, it would never enter the ElseIf clauses, so the other folders would not be created. I don't see a way to simplify this due to the non-existence of ghi not telling us if the other exist or don't exist (they may exist - and only ghi not exists, or they may also not exist).
 
Last edited:
Upvote 0
OK, I see. One more question, what if abc or def are already present. Will that cause any error? If so, what do I need to add to so it ignores the message and continues with the rest of the script?
 
Upvote 0
Simply ignoring the error would work, as xenou has posted. If you're looking for condensed code, that's about as condensed as you're gonna get. :)
 
Upvote 0
Your code as written would fail because if abc doesn't exist, it would create it. But then, it would never enter the ElseIf clauses, so the other folders would not be created.
Yeah, I figured that was probably the case as soon as I wrote it and edited my post to pose it as a question.

But thank you both, it's working fine now. I manually created the first two folders, placed several files in each folder but did not create the third folder. The script ran through each of the MkDir commands, created the third folder and left the other files/folders intact.

Just for my information, what does the last command tell it to do and under what circumstances would it be true?
Code:
On Error GoTo 0
 
Upvote 0
It basically resets the error handler, so it will function by default (as opposed to "Resume Next" which will continue on past the error and not stop).
 
Upvote 0
Thanks for the help and the explanation!
 
Upvote 0

Forum statistics

Threads
1,213,558
Messages
6,114,297
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top