# Creating folders

#### KGee

##### Well-known Member
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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

#### Zack Barresse

##### MrExcel MVP
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>

#### xenou

##### MrExcel MVP
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:

#### KGee

##### Well-known Member
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?

#### Zack Barresse

##### MrExcel MVP

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.

#### KGee

##### Well-known Member
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

#### Zack Barresse

##### MrExcel MVP
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).

#### KGee

##### Well-known Member
Thanks for the help and the explanation!

Replies
3
Views
480
Replies
4
Views
264
Replies
1
Views
158
Replies
1
Views
170
Replies
0
Views
1K

1,130,448
Messages
5,642,218
Members
417,262
Latest member
andrewd1

### 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.

### Which adblocker are you using?

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

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