Get a string

  • Thread starter Carlos Chalhoub
  • Start date
C

Carlos Chalhoub

Hi Listmates,

I need to get a string from a full path. I have captured the path but need
to isolate one specific folder. Example: if the full path is C:\Release
6.4.0\Functionals\Data Design\DS19530.doc, I need to capture "Data Design",
I've tried a combination of Left, Mid, Right, InStr, InStrRev, but I can't
get it to work. Any help is appreciated.

Carlos
 
A

Andrew Savikas

Hi Carlos,

Try using the Split function:

Sub Foo()
Dim sFilePath As String
Dim str() As String
sFilePath = "C:\Release6.4.0\Functionals\Data
Design\DS19530.doc"
str = Split(str, "\")
MsgBox str(3)
End Sub

HTH,
Andrew Savikas
 
J

Jay Freedman

Hi Carlos

I assume you're looking for the name of the folder that contains the file.
Try this:

Sub test()
MsgBox FolderOnly("C:\Release 6.4.0\Functionals\Data Design\DS19530.doc")
MsgBox FolderOnly("Data Design\DS19530.doc")
MsgBox FolderOnly("DS19530.doc")
End Sub

Private Function FolderOnly(FileNm As String) As String
Dim FolderNm As String
Dim SlashStart As Integer, SlashEnd As Integer

FolderNm = ""
SlashEnd = InStrRev(FileNm, "\")
If SlashEnd > 1 Then
SlashStart = InStrRev(FileNm, "\", SlashEnd - 1)
FolderNm = Mid$(FileNm, SlashStart + 1, _
SlashEnd - SlashStart - 1)
End If
FolderOnly = FolderNm
End Function
 

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

Top