|
-
Jun 25th, 2007, 05:16 AM
#1
Thread Starter
Lively Member
[RESOLVED] How to extract filename from given path
I have a path such as
C:\Documents and Settings\Vinitha_mani\Desktop\Annual_Product_Sales.imr
from this i want to extract "Annual_Product_Sales.imr" this......Any help on this??????
-
Jun 25th, 2007, 05:22 AM
#2
Re: How to extract filename from given path
One of several Methods
Code:
Dim sFilenamewithpath As String
Dim sFilename As String
sFilenamewithpath = "C:\Documents and Settings\Vinitha_mani\Desktop\Annual_Product_Sales.imr"
sFilename = Mid(sFilenamewithpath, InStrRev(sFilenamewithpath, "\") + 1, Len(sFilenamewithpath))
MsgBox (sFilename)
Another Method
Code:
Dim sFilename As String
Dim fso As New FileSystemObject
sFilename = fso.GetFileName("C:\Documents and Settings\Vinitha_mani\Desktop\Annual_Product_Sales.imr")
MsgBox (sFilename)
Set fso = Nothing
Last edited by danasegarane; Jun 25th, 2007 at 05:33 AM.
Please mark you thread resolved using the Thread Tools as shown
-
Jun 25th, 2007, 10:38 AM
#3
Re: How to extract filename from given path
Yet another method. (This gets asked regularly)
Code:
Option Explicit
Private Sub Form_Load()
MsgBox GetDirectoryFromPath("E:\VideoPaper\Videos\Atomic Kitten - Whole Again.mpg")
MsgBox GetFilenameFromPath("E:\VideoPaper\Videos\Atomic Kitten - Whole Again.mpg")
End Sub
Public Function GetDirectoryFromPath(FullPath As String) As String
'Returns "E:\VideoPaper\Videos\".
GetDirectoryFromPath = Left(FullPath, InStrRev(FullPath, "\"))
End Function
Public Function GetFilenameFromPath(FullPath As String) As String
'Returns "Atomic Kitten - Whole Again.mpg".
GetFilenameFromPath = Right(FullPath, Len(FullPath) - InStrRev(FullPath, "\"))
End Function
Last edited by schoolbusdriver; Jun 25th, 2007 at 11:23 AM.
Reason: give more meaningful function names
-
Jun 25th, 2007, 10:54 AM
#4
Re: How to extract filename from given path
MsgBox Mid(myStr, InStrRev(myStr, "\") + 1, Len(myStr))
-
Jun 25th, 2007, 01:44 PM
#5
Re: How to extract filename from given path
Sometime VB gives you too many options.
Code:
Private Sub Command1_Click()
Dim strFile As String
Dim strArr() As String
strFile = "C:\Documents and Settings\Vinitha_mani\Desktop\Annual_Product_Sales.imr"
strArr = Split(strFile, "\")
MsgBox strArr(UBound(strArr))
End Sub
-
Jun 26th, 2007, 12:24 AM
#6
Thread Starter
Lively Member
Re: How to extract filename from given path
Thts gr8 to try soo many options!!!!!!!!!! Thanks a lot!!!!!!!!!!!!
-
Jun 7th, 2012, 08:13 AM
#7
Lively Member
Re: [RESOLVED] How to extract filename from given path
Public Function GetFileName(flname As String) As String
'Get the filename without the path or extension.
'Input Values:
' flname - path and filename of file.
'Return Value:
' GetFileName - name of file without the extension.
Dim posn As Integer, i As Integer
Dim fName As String
posn = 0
'find the position of the last "\" character in filename
For i = 1 To Len(flname)
If (Mid(flname, i, 1) = "\") Then posn = i
Next i
'get filename without path
fName = Right(flname, Len(flname) - posn)
'get filename without extension
posn = InStr(fName, ".")
If posn <> 0 Then
fName = Left(fName, posn - 1)
End If
GetFileName = fName
End Function
-
Jun 7th, 2012, 08:36 AM
#8
Re: [RESOLVED] How to extract filename from given path
@mahesh,
see the date of the post, it is very old post year of 2007.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|