Link to home
Start Free TrialLog in
Avatar of gordonrr
gordonrr

asked on

How do I extract a directory from a path?

I need to check for the existence of a directory.  If the directory doesn't exist, it  will create the directory & write (or copy files) to it.  If the directory does exist, it will not create the directory but will still write files into that directory.  The name of the directory that will be created (if it hasn't already been) will come from a field in an access table.  I am using VBA in Access 2002.  If anyone can supply some sample code, it will be greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of List244
List244

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of List244
List244

Yeah, Idle_Mind is right about that, if you are working with multiple layers of folders, it will not work.
You can do it without the APIs if you want.

I've seen solutions that Split() the path using backslash as the delimiter and then "build up" the path incrementally calling the default MkDir() as you go.
You could do this fairly simple function:

Public Function MakeDir(Dir As String)
On Error Resume Next
Dim i As Long
Dim Folder() As String
Folder = Split(Dir, "\") //Split it into segments
Dir = Folder(0) & "\" //Put it to the start
For i = 0 To UBound(Folder)
    MkDir Dir //Try making the directory
    Dir = Dir & (Folder(i + 1)) & "\"
Next i
End Function

(As Idle_mind mentioned)
PS: The first function I gave you should be:

Public Function MakeDir(Dir As String)
On Error Resume Next
    MkDir Dir
End Function

'Mkdir would conflict so i changed the name

Oh... and the second should be:

Public Function MakeDir(Dir As String)
On Error Resume Next
Dim i As Long
Dim Folder() As String
Folder = Split(Dir, "\") 'Split it into segments
Dir = Folder(0) & "\" ''Put it to the start
For i = 0 To UBound(Folder)
    MkDir Dir 'Try making the directory
    Dir = Dir & (Folder(i + 1)) & "\"
Next i
End Function

Too much C++ lately, // is addictive..