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??????
Printable View
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??????
One of several Methods
Another MethodCode: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)
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
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
MsgBox Mid(myStr, InStrRev(myStr, "\") + 1, Len(myStr))
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
Thts gr8 to try soo many options!!!!!!!!!! Thanks a lot!!!!!!!!!!!!
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
@mahesh,
see the date of the post, it is very old post year of 2007.