Extract filename from path

后端 未结 8 663
梦谈多话
梦谈多话 2021-01-04 08:21

I need to extract the filename from a path (a string):

e.g., \"C:\\folder\\folder\\folder\\file.txt\" = \"file\" (or even \"file.txt\" to get me sta

相关标签:
8条回答
  • 2021-01-04 08:27

    Here's simpler method: a one-line function to extract only the name — without the file extension — as you specified in your example:

    Function getName(pf):getName=Split(Mid(pf,InStrRev(pf,"\")+1),".")(0):End Function
    

    ...so, using your example, this:

           MsgBox getName("C:\folder\folder\folder\file.txt")

      returns:

           "file"

    For cases where you want to extract the filename while retaining the file extension, or if you want to extract the only the path, here are two more single-line functions:

    Extract Filename from x:\path\filename:

    Function getFName(pf)As String:getFName=Mid(pf,InStrRev(pf,"\")+1):End Function
    

    Extract Path from x:\path\filename:

    Function getPath(pf)As String: getPath=Left(pf,InStrRev(pf,"\")): End Function
    

    Examples:
    examples

    (Source)

    0 讨论(0)
  • 2021-01-04 08:27

    I used kaveman's suggestion successfully as well to get the Full File name but sometimes when i have lots of Dots in my Full File Name, I used the following to get rid of the .txt bit:

    FileName = Left(FullFileName, (InStrRev(FullFileName, ".") - 1))
    
    0 讨论(0)
  • 2021-01-04 08:32

    Thanks to kaveman for the help. Here is the full code I used to remove both the path and the extension (it is not full proof, does not take into consideration files that contain more than 2 decimals eg. *.tar.gz)

    sFullPath = "C:\dir\dir\dir\file.txt"   
    sFullFilename = Right(sFullPath, Len(sFullPath) - InStrRev(sFullPath, "\"))
    sFilename = Left(sFullFilename, (InStr(sFullFilename, ".") - 1))
    

    sFilename = "file"

    0 讨论(0)
  • 2021-01-04 08:38

    `You can also try:

    Sub filen() Dim parts() As String Dim Inputfolder As String, a As String 'Takes input as any file on disk Inputfolder = Application.GetOpenFilename("Folder, *") parts = Split(Inputfolder, "\") a = parts(UBound(parts())) MsgBox ("File is: " & a) End Sub

    This sub can display Folder name of any file

    0 讨论(0)
  • 2021-01-04 08:40

    I was looking for a solution without code. This VBA works in the Excel Formula Bar:

    To extract the file name:

    =RIGHT(A1,LEN(A1)-FIND("~",SUBSTITUTE(A1,"\","~",LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))))
    

    To extract the file path:

    =MID(A1,1,LEN(A1)-LEN(MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"\",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))+1,LEN(A1))))
    
    0 讨论(0)
  • 2021-01-04 08:44

    You can use a FileSystemObject for that.

    First, include a reference for de Microsoft Scripting Runtime (VB Editor Menu Bar > Tools > References).

    After that, you can use a function such as this one:

    Function Get_FileName_fromPath(myPath as string) as string
        Dim FSO as New Scripting.FileSystemObject
    
        'Check if File Exists before getting the name
        iF FSO.FileExists(myPath) then
            Get_FileName_fromPath = FSO.GetFileName(myPath)
        Else
            Get_FileName_fromPath = "File not found!"
        End if
    End Function
    

    File System Objects are very useful for file manipulation, especially when checking for their existence and moving them around. I like to use them early bound (Dim statement), but you can use them late bound if you prefer (CreateObject statement).

    0 讨论(0)
提交回复
热议问题