Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 170
Default How to split filename from filepath?

If I retrieve a filename including the filepath like this:

strFileName = Application.GetOpenFilename("Report (*.txt; *.dat ),*.txt;
*.dat")

How do I manipulate the strFileName, retrieving only the filename?

(guess I have to search from right to find the first "\". Then delete the
content to the left of this character. But I don't know how this works in VBA)

Thanks for suggestions.

Frank
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default How to split filename from filepath?

Hi Frank

You can use dir and use the left function to remove the extension

Sub test()
Dim FName As Variant
Dim MyPath As String
Dim SaveDriveDir As String

SaveDriveDir = CurDir

MyPath = ThisWorkbook.Path
ChDrive MyPath
ChDir MyPath

FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xls), *.xls")
If FName < False Then
MsgBox Dir(FName)
End If

ChDrive SaveDriveDir
ChDir SaveDriveDir
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Frank" wrote in message ...
If I retrieve a filename including the filepath like this:

strFileName = Application.GetOpenFilename("Report (*.txt; *.dat ),*.txt;
*.dat")

How do I manipulate the strFileName, retrieving only the filename?

(guess I have to search from right to find the first "\". Then delete the
content to the left of this character. But I don't know how this works in VBA)

Thanks for suggestions.

Frank



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default How to split filename from filepath?

I usually impliment it as a function...

Public Function GetFileName(ByVal FullName As String) As String
GetFileName = Right(FullName, Len(FullName) - InStrRev(FullName, "\"))
End Function
--
HTH...

Jim Thomlinson


"Frank" wrote:

If I retrieve a filename including the filepath like this:

strFileName = Application.GetOpenFilename("Report (*.txt; *.dat ),*.txt;
*.dat")

How do I manipulate the strFileName, retrieving only the filename?

(guess I have to search from right to find the first "\". Then delete the
content to the left of this character. But I don't know how this works in VBA)

Thanks for suggestions.

Frank

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default How to split filename from filepath?

Something like this will do it:

Function FileFromPath(ByVal strFullPath As String, _
Optional bExtensionOff As Boolean = False) As String

Dim FPL As Long 'len of full path
Dim PLS As Long 'position of last slash
Dim pd As Long 'position of dot before exension
Dim strFile As String

On Error GoTo ERROROUT

FPL = Len(strFullPath)
PLS = InStrRev(strFullPath, "\", , vbBinaryCompare)
strFile = Right$(strFullPath, FPL - PLS)

If bExtensionOff = False Then
FileFromPath = strFile
Else
pd = InStr(1, strFile, ".", vbBinaryCompare)
FileFromPath = Left$(strFile, pd - 1)
End If

Exit Function
ERROROUT:

On Error GoTo 0
FileFromPath = ""

End Function


RBS


"Frank" wrote in message
...
If I retrieve a filename including the filepath like this:

strFileName = Application.GetOpenFilename("Report (*.txt; *.dat ),*.txt;
*.dat")

How do I manipulate the strFileName, retrieving only the filename?

(guess I have to search from right to find the first "\". Then delete the
content to the left of this character. But I don't know how this works in
VBA)

Thanks for suggestions.

Frank


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default How to split filename from filepath?

One more way:

Option Explicit
Sub testme()
Dim strFileName As String
Dim mySplit As Variant
strFileName = "c:\somepath\anotherpath\filename.xls"

mySplit = Split(strFileName, "\")
'mySplit = Split97(strFileName, "\")
MsgBox mySplit(UBound(mySplit))

End Sub

Function Split97(sStr As String, sdelim As String) As Variant
'from Tom Ogilvy
Split97 = Evaluate("{""" & _
Application.Substitute(sStr, sdelim, """,""") & """}")
End Function


Split was added in xl2k. If you're using xl97, use Tom's split97. If you and
your users are all at xl2k or higher, you can delete that function completely.



Frank wrote:

If I retrieve a filename including the filepath like this:

strFileName = Application.GetOpenFilename("Report (*.txt; *.dat ),*.txt;
*.dat")

How do I manipulate the strFileName, retrieving only the filename?

(guess I have to search from right to find the first "\". Then delete the
content to the left of this character. But I don't know how this works in VBA)

Thanks for suggestions.

Frank


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 923
Default How to split filename from filepath?

Also note that InStrRev is not implimented in xl97 so use Dave Peterson /
Tom Olgivy methods

--
Cheers
Nigel



"Dave Peterson" wrote in message
...
One more way:

Option Explicit
Sub testme()
Dim strFileName As String
Dim mySplit As Variant
strFileName = "c:\somepath\anotherpath\filename.xls"

mySplit = Split(strFileName, "\")
'mySplit = Split97(strFileName, "\")
MsgBox mySplit(UBound(mySplit))

End Sub

Function Split97(sStr As String, sdelim As String) As Variant
'from Tom Ogilvy
Split97 = Evaluate("{""" & _
Application.Substitute(sStr, sdelim, """,""") & """}")
End Function


Split was added in xl2k. If you're using xl97, use Tom's split97. If you

and
your users are all at xl2k or higher, you can delete that function

completely.



Frank wrote:

If I retrieve a filename including the filepath like this:

strFileName = Application.GetOpenFilename("Report (*.txt; *.dat ),*.txt;
*.dat")

How do I manipulate the strFileName, retrieving only the filename?

(guess I have to search from right to find the first "\". Then delete

the
content to the left of this character. But I don't know how this works

in VBA)

Thanks for suggestions.

Frank


--

Dave Peterson



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filename extract from Filepath Text String DaveyC Excel Discussion (Misc queries) 4 December 20th 07 04:04 PM
Filepath Marie-Jo Excel Discussion (Misc queries) 3 July 5th 05 07:56 PM
Function to return filepath given full filename Matt Lawson[_3_] Excel Programming 8 March 3rd 04 02:32 AM
get filename and filepath Robert Ehrlich Excel Programming 3 December 21st 03 12:23 PM
Split fullname into Drive, Path and Filename Michael Göhring Excel Programming 2 December 12th 03 12:56 PM


All times are GMT +1. The time now is 04:54 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"