Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stripping text 1

Status
Not open for further replies.

EscapeUK

Programmer
Jul 7, 2000
438
GB
I have some text strings which contain a file and its full path.

For example


c:\User1\this is a directory\myfile.doc
c:\Userxyz\123456789\abcderfgh\folder1.xls
c:\floder1\qwerty.exe

How to do just get back the file names not the path.
 
Try this out:


Option Explicit

Private Sub Form_Load()
Debug.Print GetFileName("c:\temp\program files\hello\my test.txt")
End Sub

Private Function GetFileName(Fullpath As String) As String
Dim i As Long
For i = Len(Fullpath) To 1 Step -1
If Mid(Fullpath, i, 1) <> &quot;/&quot; And Mid(Fullpath, i, 1) <> &quot;\&quot; Then
GetFileName = Mid(Fullpath, i, 1) & GetFileName
Else
Exit For
End If
Next i
End Function
 
Hi,

str2 = &quot;c:\User1\this is a directory\myfile.doc&quot;
str1() = Split(str2, &quot;\&quot;)
intC = UBound(str1)
Debug.Print str1(intC)

Jon
 
With a slight modification to kieren's function, you can without the having to loop, extract the filename as follows:

This will work regarless of whether forward or backward slashes are used in the pathnam.


Private Function GetFileName(Fullpath As String) As String

Dim i As Long

i = InStrRev(Fullpath, &quot;\&quot;)
If (i > 0) then
GetFileName = mid(Fullpath, i + 1)
Else
i = InStrRev(Fullpath, &quot;/&quot;)
If (i > 0) then
GetFileName = mid(Fullpath, i + 1)
Else
GetFileName = Fullpath
End If
End If

End Function
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
And, as an alternative, a FileSystemObject solution:
[tt]
Private Function GetFileName(strPath As String) As String
Dim fso As Object
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
If fso.FileExists(strPath) Then
GetFileName = fso.GetFile(strPath).Name
End If
Set fso = Nothing
End Function
 
(one makor drawback of the fso solution is that it requires the files to actually exist before it can resolve the name correctly)
 
So here's a corrected version:
[tt]
Private Function GetFileName(strPath As String) As String
Dim fso As Object
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
GetFileName = fso.GetFileName(strPath)
End Function
 
Here's the simplest version where sPath is the full path and sFile is string to put in the filename:

sFile = mid(sPath,instrrev(sPath,&quot;\&quot;)+1,len(sPath))

-Eric
 
And let's pretend some user taps in &quot;c:\test.txt\&quot; as the file name...
 
Well obviously you should test if the filepath is a valid file and such before calling this method. That is just a given. The box which accepts the user's input should make the validation checking there before passing it on to other parts of the program.

If the path is &quot;&quot; or right(trim(mypath),1) = &quot;\&quot; then trim it or don't call the function.

:)
 
Code:
Public Function basFileFromPath(strPath As String) As String

    'Michael L. Red Tek-Tips thread222-262607
    'Adapted from jon4747 and CajunCenturion

    '? basFileFromPath(&quot;c:\My Documents\Ms_Access\Pervasive.MDB&quot;)
    'Pervasive.MDB

    '? basFileFromPath(&quot;c:\My Documents/Ms_Access/Pervasive.MDB&quot;)
    'Pervasive.MDB


    Dim MyFil As Variant
    Dim MyPath As String

    MyPath = Replace(strPath, &quot;/&quot;, &quot;\&quot;)
    MyFil = Split(MyPath, &quot;\&quot;)
    If (Len(Trim(MyFil(UBound(MyFil)))) = 0) Then
        basFileFromPath = &quot;Invalid Path&quot;
     Else
        basFileFromPath = MyFil(UBound(MyFil))
    End If

End Function
MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Sure, but the fso solution does all this work for you. Why reinvent the wheel?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top