Pages Menu
Categories Menu

Posted by on Sep 21, 2013 in Code Snippets | 0 comments

Parsing a File String Into Path, File Name, and Extension

Parsing a File String Into Path, File Name, and Extension

Sample 1: Extract a File name that includes the file extension

Function FileNameFromPath(strFullPath As String) As String

     FileNameFromPath = Right(strFullPath, Len(strFullPath) - InStrRev(strFullPath, "\"))

End Function

 

Sample 2: Extract a File name that does not include the file extension

Function FileNameNoExtensionFromPath(strFullPath As String) As String

    Dim intStartLoc As Integer
    Dim intEndLoc As Integer
    Dim intLength As Integer

    intStartLoc = Len(strFullPath) - (Len(strFullPath) - InStrRev(strFullPath, "\") - 1)
    intEndLoc = Len(strFullPath) - (Len(strFullPath) - InStrRev(strFullPath, "."))
    intLength = intEndLoc - intStartLoc

    FileNameNoExtensionFromPath = Mid(strFullPath, intStartLoc, intLength)

End Function

 

 

Sample 3: Extract a path to the containing folder (last character is a slash)

Function FolderFromPath(ByRef strFullPath As String) As String

     FolderFromPath = Left(strFullPath, InStrRev(strFullPath, "\"))

End Function

 

Sample 4: Extract a file extension (no dot)

Function FileExtensionFromPath(ByRef strFullPath As String) As String

     FileExtensionFromPath = Right(strFullPath, Len(strFullPath) - InStrRev(strFullPath, "."))

End Function