Here are three functions that will verify a path exists and attempt to create the path (including subfolders) if it does not:
- VerifyFolder(): uses recursion and the FileSystemObject
- MakeSurePathExists(): uses the MakeSureDirectoryPathExists API function
- EnsurePathExists(): uses the Unicode-safe SHCreateDirectoryExW API function
I've used this function for 15 years, so it is well-tested. It also makes for a great introduction to the topic of recursion. That said, the Windows API options almost certainly perform better.
This approach is based on an API function–
MakeSureDirectoryPathExists–that does exactly what it says. There is something to be said for that sort of simplicity.
That said, its major limitation is that it does not support Unicode paths.
This approach relies on the
SHCreateDirectoryExW API function to create directories if they do not exist.
What Should You Use?
The short answer? EnsurePathExists.
I recommend the EnsurePathExists function as it provides full Unicode-compatibility along with the superior performance of a Windows API function.
However, if you want to learn more about recursion, the VerifyFolder function is a great practical example of how recursion can make certain classes of problems much easier to solve.