Creating folders

KGee

Well-known Member
Joined
Nov 26, 2008
Messages
530
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

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
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
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
Joined
Mar 2, 2007
Messages
16,832
Office Version
  1. 2019
Platform
  1. Windows
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
Joined
Nov 26, 2008
Messages
530
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web

ADVERTISEMENT

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
Joined
Nov 26, 2008
Messages
530
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
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
Joined
Nov 26, 2008
Messages
530
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Thanks for the help and the explanation!
 

Watch MrExcel Video

Forum statistics

Threads
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.
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
Top