Results 1 to 8 of 8

Thread: [RESOLVED] How to extract filename from given path

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2007
    Posts
    76

    Resolved [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??????

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    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

  3. #3
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    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

  4. #4
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: How to extract filename from given path

    MsgBox Mid(myStr, InStrRev(myStr, "\") + 1, Len(myStr))

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2007
    Posts
    76

    Re: How to extract filename from given path

    Thts gr8 to try soo many options!!!!!!!!!! Thanks a lot!!!!!!!!!!!!

  7. #7
    Lively Member
    Join Date
    Oct 2011
    Posts
    114

    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

  8. #8
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: [RESOLVED] How to extract filename from given path

    @mahesh,
    see the date of the post, it is very old post year of 2007.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width