Using MkDir to Create Multiple Directories

CuriousOne1
CuriousOne1 used Ask the Experts™
on
Hi,

I've been using MkDir for some time in my code to create directories. However, I have discovered the limitation of this command in that it will only create 1 directory at a time. If I want to create multiple directories at a time, the function fails to perform.

E.g.

If my hard disk has folder path: C:\Test

But I want to create the following directory: C:\Test\A\Apples

In order for MkDir to work, I need to have first created the C:\Test\A directory.

I found some a code to solve the problem on this website: http://www.cpearson.com/excel/MakeDirMulti.htm

But when adding code to my application and compiling, the compiler stops on the following line of code:

"Application.PathSeparator"

ERROR = "Method or Data member not found".

I think it's because I haven't enabled the Reference for this function. If that's correct, what Reference am I missing?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
place this declaration on top of a regular module, below the Option declaration I.e.,

Option compare database

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


now, to use the function

                call makesuredirectorypathexists "C:\Test\A\Apples\"

just make sure that the string ends with "\"

and will create all the directories


DOS command "md" does NOT require you to create C:\Test\A before you can create C:\Test\A\Apples
If your app is going to run on Windows machines, then ... why don't you execute the MD DOS command from inside your app. Some functions are better for executing executables, while others are better for executing a DOS command. I'm sorry I don't know which one is which.

Author

Commented:
Great Capricorn it works fine.

So by declaring that 1 line function it references the library to create the full path (how does it know where to find the DLL flie)?.

Can I avoid declaring references if I was to declare my functions in this way?

How can I find out more about DLL files that are available and what they can do?



samenglish, I can't see anything much simplier than what Capricorn provided. Show me an example if it's simpler.

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Another one to use:

Option Explicit
 
Private Sub Command1_Click()
    CreatePath "C:\Test\A\Apples"
End Sub
 
Private Sub CreatePath(ByVal fullPathToCreate As String)
    Dim i As Integer
    Dim path As String
    Dim fso As Object
    Dim folders() As String
        
    Set fso = CreateObject("Scripting.FileSystemObject")
    folders = Split(fullPathToCreate, "\")
    For i = LBound(folders) To UBound(folders)
        If folders(i) <> "" Then
            path = path & folders(i) & "\"
            If (Not fso.FolderExists(path)) Then
                fso.CreateFolder path
            End If
        End If
    Next
End Sub

Open in new window

Author

Commented:
Oh, I thought that was a custom made function, MakeSureDirectoryPathExists, but after searching MSDN website (http://msdn.microsoft.com/en-us/library/ms679291(VS.85).aspx) I found it is a windows function. So I've answered my questions 1 & 3 above.

It seems limited though in what windows functions are available. I don't think I can get away from declaring References in all cases. Any comments ...

Top Expert 2016

Commented:
CuriousOne1,

isn't what  i posted gave you the answer to what you want to do?

<I want to create multiple directories at a time>

Author

Commented:
Yes, just thought whilst we're on the subject you could clarify your solution. But yes, you answered the initial question.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
"I don't think I can get away from declaring References in all cases. Any comments ... "

If you don't want any external references then you'll have to loop thru the folder path and make each directory with the built-in MkDir...like in your originally linked example.  =)

Author

Commented:
oh okay, thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial