Extract filename from path

只愿长相守 提交于 2019-12-18 19:39:20

问题


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 started)

Essentially everything before and including the last \

I've heard of using wildcards in place of Regex (as it's an odd implementation in VBA?) but can't find anything solid.

Cheers in advance.


回答1:


I believe this works, using VBA:

Dim strPath As String
strPath = "C:\folder\folder\folder\file.txt"

Dim strFile As String
strFile = Right(strPath, Len(strPath) - InStrRev(strPath, "\"))

InStrRev looks for the first instance of "\" from the end, and returns the position. Right makes a substring starting from the right of given length, so you calculate the needed length using Len - InStrRev




回答2:


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"




回答3:


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))))



回答4:


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:

       

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:

(Source)




回答5:


Using Java:

String myPath="C:\folder\folder\folder\file.txt";
System.out.println("filename " +  myPath.lastIndexOf('\\'));



回答6:


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))



回答7:


`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




回答8:


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).



来源:https://stackoverflow.com/questions/5932909/extract-filename-from-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!