[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I extract a directory from a path?

Posted on 2006-05-18
10
Medium Priority
?
418 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:gordonrr
  • 4
  • 2
7 Comments
 
LVL 8

Accepted Solution

by:
List244 earned 672 total points
ID: 16714235
Why not a function along the lines of:

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

If the directory doesn't exist, the function wont make it, if it does, it will.
0
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 664 total points
ID: 16714507
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 664 total points
ID: 16714530
The problem with MkDir() is that you can't create an entire directory structure in one shot.  It errors out if the "parent" folders don't exist.

You can use the SHCreateDirectory() API to create an entire structure in one shot:

Option Explicit

Private Declare Function PathFileExistsW Lib "shlwapi.dll" (ByVal path As Long) As Boolean
Private Declare Function SHCreateDirectory Lib "shell32" (ByVal hWnd As Long, ByVal path As Long) As Long

Public Sub MkDirPath(ByVal path As String)
    If Not PathFileExistsW(StrPtr(path)) Then
        SHCreateDirectory ByVal 0&, StrPtr(path)
    End If
End Sub

Private Sub Command1_Click()
    Dim path As String
    path = "c:\test\sub\folder"
    MkDirPath path
End Sub
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:List244
ID: 16714549
Yeah, Idle_Mind is right about that, if you are working with multiple layers of folders, it will not work.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16714565
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.
0
 
LVL 8

Expert Comment

by:List244
ID: 16714626
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)
0
 
LVL 8

Expert Comment

by:List244
ID: 16714632
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..
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Simple Linear Regression

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question