Extracting path from FileName (1 Viewer)

abubasil

Registered User.
Local time
Today, 10:12
Joined
Aug 31, 2011
Messages
29
Hi everyone

this is my own easy way to extract directories from file name
PHP:
Private Sub doit_Click()
Dim rght As String
Dim lft As String
Dim fnl As String
Dim i As Integer
i = 1
Do Until lft = "\"
rght = Right(Me.pth.Value, i)
lft = Left(rght, 1)
i = i + 1
Loop
fnl = Replace(Me.pth.Value, rght, "")
MsgBox fnl
End Sub

it may sound naive but it does work , try the sample in the attachment
I am not smart enough to put it in the form of user's function
would you please do it for the sake of all

Thank you
 

Attachments

  • extract-file-path.zip
    12.6 KB · Views: 96

boblarson

Smeghead
Local time
Today, 00:12
Joined
Jan 12, 2001
Messages
32,059
A much simpler way is:

Code:
Function GetDirectories(strPath As String) As Variant
    Dim varSplit As Variant
    Dim lngCount As Long
    Dim strHold As String
 
    varSplit = Split(strPath, "\")
 
    For lngCount = 1 To UBound(varSplit) - 1
        strHold = strHold & varSplit(lngCount) & vbCrLf
    Next
 
    GetDirectories = strHold
End Function

So something like this:

?GetDirectories("C:\Users\My Documents\2012\April\Data\SalesNumbers.xls")

would yield

Users
My Documents
2012
April
Data
 

abubasil

Registered User.
Local time
Today, 10:12
Joined
Aug 31, 2011
Messages
29
Thank you for your code.. For me I was working on archive program and I have the path of a picture file and I needed to locate its folder and open it with shell , explorer.exe and passing the path as parameter to the explorer ...., so the letter of the drive is needed and I would not drop it nor to drop the other slashes, thank you
 
Last edited:

boblarson

Smeghead
Local time
Today, 00:12
Joined
Jan 12, 2001
Messages
32,059
Thank you for your code.. For me I was working on archive program and I have the path of a picture file and I needed to locate its folder and open it with shell , explorer.exe and passing the path as parameter to the explorer ...., so the letter of the drive is needed and I would not drop it, thank you

If you just need the last folder for opening with Shell, then you could modify my code to this:

Code:
Function GetPathFolder(strPath As String) As String
    Dim strFolderPath As String
 
    strFolderPath = Left(strPath, InStrRev(strPath, "\", , vbTextCompare))
 
    GetPathFolder = strFolderPath
End Function

So using this (in the Immediate Window for testing):

?GetPathFolder("C:\Users\My Documents\Something\myfile.txt")

Returns this:

C:\Users\My Documents\Something\
 

abubasil

Registered User.
Local time
Today, 10:12
Joined
Aug 31, 2011
Messages
29
Really amazing , only three lines code ! Thank you so much
 

MarkK

bit cruncher
Local time
Today, 00:12
Joined
Mar 17, 2004
Messages
8,186
Simplifying Bob's code some more...
Code:
Function GetPathFolder(strPath As String) As String
  GetPathFolder = Left(strPath, InStrRev(strPath, "\", , vbTextCompare))
End Function
A Scripting.FileSystemObject also has a GetParentFolderName() method, so you could do...
Code:
Function GetPathFolder(strPath As String) As String
  GetPathFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(strPath)
End Function
Mark
 

boblarson

Smeghead
Local time
Today, 00:12
Joined
Jan 12, 2001
Messages
32,059
Simplifying Bob's code some more...
Code:
Function GetPathFolder(strPath As String) As String
  GetPathFolder = Left(strPath, InStrRev(strPath, "\", , vbTextCompare))
End Function
A Scripting.FileSystemObject also has a GetParentFolderName() method, so you could do...
Code:
Function GetPathFolder(strPath As String) As String
  GetPathFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(strPath)
End Function
Mark

Although that probably won't work with the Runtime if you are using that. It takes exception to CreateObject :D
 
Last edited:

Users who are viewing this thread

Top Bottom