PDA

View Full Version : Solved: macro to create a directory



cmefly
01-14-2005, 12:24 PM
Hi,

how do i write a macro to create a directory on my H: drive?

specificall, a folder called "data extractor"

thanks,

marc

cmefly
01-14-2005, 12:31 PM
Better yet....
how do you go about writing code for:

"if the directory called "data extractor" does not exist....create it....."

Ken Puls
01-14-2005, 12:33 PM
Hi Marc,

The simple code to do it is:

MkDir "H:\data extractor"

I only say simple, as you may want to implement some error handling in case it already exists. Like:

Sub CreateDirectory()
On Error Resume Next
MkDir "H:\data extractor"
If Err.Number <> 0 Then MsgBox "Directory exists!"
On Error GoTo 0

End Sub

Cheers,

Ken Puls
01-14-2005, 12:33 PM
:rotlaugh:

You're too fast! Use the second option. It will accomplish what you're after!

cmefly
01-14-2005, 12:36 PM
Awesome!!! just Awesome!!

thank you!

Ken Puls
01-14-2005, 12:40 PM
You're welcome,

Now you get VBAX lesson 2, though! ;)

See up the top of the thread, there's a Thread Tools menu? You can jump in there and mark your own threads solved. Another slick tool developed by our board coders! :)

Cheers,

NAW
01-08-2021, 06:04 AM
This recursively creates directories - eg:

NW_MkDir("C:\The\Quick\Brown\Fox\Jumped\Over\The\Lazy\Dog")




Function NW_MkDir(NewFolderPath As String) As Boolean
On Error Resume Next
NW_PATH = Split(NewFolderPath, "\")
For Each NW_FOLDER In NW_PATH
NW_FOLDERS = NW_FOLDERS & "\" & NW_FOLDER
' MsgBox Mid(NW_FOLDERS, 2, 999)
MkDir (Mid(NW_FOLDERS, 2, 999))
Next
If Dir(NW_FOLDER, 16) = "" Then
NW_MkDir = False
Else: NW_MkDir = True
End If
End Function

snb
01-08-2021, 06:33 AM
Wrong timing (15 years), wrong method (recursion), wrong code.