For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic Syntax > November 2005 > Extract filename from path









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Extract filename from path
Drew

2005-11-14, 7:04 pm

Is there a simple VB way of extracting the filename from a full path string?

Thanks,
Drew


Bob Butler

2005-11-14, 7:04 pm

"Drew" <drew.myers@esrd.com> wrote in message
news:%23b%235AqV6FHA.1720@TK2MSFTNGP09.phx.gbl
> Is there a simple VB way of extracting the filename from a full path
> string?


how about

sFile=mid$(sPath,InstrRev(sPath,"\")+1)

--
Reply to the group so all can participate
VB.Net: "Fool me once..."

Ken Halter

2005-11-14, 7:04 pm

"Drew" <drew.myers@esrd.com> wrote in message
news:%23b%235AqV6FHA.1720@TK2MSFTNGP09.phx.gbl...
> Is there a simple VB way of extracting the filename from a full path
> string?
>
> Thanks,
> Drew


Pretty simple... this'll get just the name or just the path or..... add more
to it yourself <g>
'===========
Option Explicit

Private Sub Command1_Click()
Dim sSomePath As String
Dim sFileName As String
Dim sPathOnly As String

sSomePath = "C:\Temp\Test\This.exe"

sFileName = ExtractFileName(sSomePath)
sPathOnly = ExtractPath(sSomePath)

MsgBox "The full path is: " & sSomePath & vbCrLf _
& "The path is: " & sPathOnly & vbCrLf _
& "The file is: " & sFileName

End Sub

Private Function ExtractFileName(FullPath As String) As String
Dim iPos As Integer
ExtractFileName = FullPath
iPos = InStrRev(FullPath, "\")
If iPos > 0 Then
ExtractFileName = Mid$(FullPath, iPos + 1)
End If
End Function

Private Function ExtractPath(FullPath As String) As String
Dim iPos As Integer
ExtractPath = FullPath
iPos = InStrRev(FullPath, "\")
If iPos > 0 Then
ExtractPath = Left$(FullPath, iPos - 1)
End If
End Function
'===========

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..


Drew

2005-11-14, 7:04 pm

Great! Thanks to both of you.

Drew

"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:OI8JnwV6FHA.1188@TK2MSFTNGP12.phx.gbl...
> "Drew" <drew.myers@esrd.com> wrote in message
> news:%23b%235AqV6FHA.1720@TK2MSFTNGP09.phx.gbl...
>
> Pretty simple... this'll get just the name or just the path or..... add
> more to it yourself <g>
> '===========
> Option Explicit
>
> Private Sub Command1_Click()
> Dim sSomePath As String
> Dim sFileName As String
> Dim sPathOnly As String
>
> sSomePath = "C:\Temp\Test\This.exe"
>
> sFileName = ExtractFileName(sSomePath)
> sPathOnly = ExtractPath(sSomePath)
>
> MsgBox "The full path is: " & sSomePath & vbCrLf _
> & "The path is: " & sPathOnly & vbCrLf _
> & "The file is: " & sFileName
>
> End Sub
>
> Private Function ExtractFileName(FullPath As String) As String
> Dim iPos As Integer
> ExtractFileName = FullPath
> iPos = InStrRev(FullPath, "\")
> If iPos > 0 Then
> ExtractFileName = Mid$(FullPath, iPos + 1)
> End If
> End Function
>
> Private Function ExtractPath(FullPath As String) As String
> Dim iPos As Integer
> ExtractPath = FullPath
> iPos = InStrRev(FullPath, "\")
> If iPos > 0 Then
> ExtractPath = Left$(FullPath, iPos - 1)
> End If
> End Function
> '===========
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Please keep all discussions in the groups..
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com