How to determine the parent directory

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dear all,

I have a string variable, i.e. strDB="c:\data\aproject\database\dbfile.mdb".
I want to add a subdirectory under "c:\data\aproject\reports"
programmatically. How can I do that?

Thanks in advance!
 
strDB = "c:\data\aproject\database\dbfile.mdb"
iPos = InStrRev(strDB, "\")
iPos = InStrRev(strDB, "\", iPos - 1)
MkDir Left(strDB, iPos) & "reports"
 
Here are two additional ideas:

Sub Demo1()
Dim v
Dim s As String
Const strDB = "c:\data\aproject\database\dbfile.mdb"

v = Split(strDB, Application.PathSeparator)
v(UBound(v)) = "Reports"
s = Join(v, Application.PathSeparator)
End Sub

Sub Demo2()
Dim fso
Dim s As String
Const strDB = "c:\data\aproject\database\dbfile.mdb"

Set fso = CreateObject("Scripting.FileSystemObject")
With fso
If .FileExists(strDB) Then
s = .BuildPath(.GetFile(strDB).ParentFolder.Path, "Reports")
End If
End With
End Sub

HTH :>)
 
Thank you very much all of you!

Dana DeLouis said:
Here are two additional ideas:

Sub Demo1()
Dim v
Dim s As String
Const strDB = "c:\data\aproject\database\dbfile.mdb"

v = Split(strDB, Application.PathSeparator)
v(UBound(v)) = "Reports"
s = Join(v, Application.PathSeparator)
End Sub

Sub Demo2()
Dim fso
Dim s As String
Const strDB = "c:\data\aproject\database\dbfile.mdb"

Set fso = CreateObject("Scripting.FileSystemObject")
With fso
If .FileExists(strDB) Then
s = .BuildPath(.GetFile(strDB).ParentFolder.Path, "Reports")
End If
End With
End Sub

HTH :>)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top