vba 从路径中提取文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5932909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract filename from path
提问by Matt Rowles
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)
例如, “C:\folder\folder\folder\file.txt” = “file”(甚至“file.txt”让我开始)
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.
我听说过使用通配符代替 Regex(因为它在 VBA 中是一个奇怪的实现?)但找不到任何可靠的东西。
Cheers in advance.
提前加油。
采纳答案by Matt Rowles
Thanks to kavemanfor 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)
感谢kaveman的帮助。这是我用来删除路径和扩展名的完整代码(它不是完整的证明,不考虑包含超过 2 个小数的文件,例如 *.tar.gz)
sFullPath = "C:\dir\dir\dir\file.txt"
sFullFilename = Right(sFullPath, Len(sFullPath) - InStrRev(sFullPath, "\"))
sFilename = Left(sFullFilename, (InStr(sFullFilename, ".") - 1))
sFilename = "file"
sFilename = "文件"
回答by kaveman
I believe this works, using VBA:
我相信这有效,使用 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 rightof given length, so you calculate the needed length using Len - InStrRev
InStrRev
从末尾查找“\”的第一个实例,并返回位置。从给定长度的右侧Right
开始创建一个子字符串,因此您可以使用Len - InStrRev
回答by live-love
I was looking for a solution without code. This VBA works in the Excel Formula Bar:
我正在寻找没有代码的解决方案。这个 VBA 在 Excel 公式栏中工作:
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))))
回答by ashleedawg
Here's simpler method: a one-line functionto extract onlythe 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")
???????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:
从
x:\path\filename
以下位置提取文件名:Function getFName(pf)As String:getFName=Mid(pf,InStrRev(pf,"\")+1):End Function
从中提取路径
x:\path\filename
:Function getPath(pf)As String: getPath=Left(pf,InStrRev(pf,"\")): End Function
例子:
(Source)
(来源)
回答by Luixv
Using Java:
使用 Java:
String myPath="C:\folder\folder\folder\file.txt";
System.out.println("filename " + myPath.lastIndexOf('\'));
回答by Alan Elston
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:
我也成功地使用了 kaveman 的建议来获取完整文件名,但有时当我的完整文件名中有很多点时,我使用以下方法去除了 .txt 位:
FileName = Left(FullFileName, (InStrRev(FullFileName, ".") - 1))
回答by Akash
`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
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
这个子可以显示任何文件的文件夹名称
回答by FCastro
You can use a FileSystemObject for that.
您可以为此使用 FileSystemObject。
First, include a reference for de Microsoft Scripting Runtime(VB Editor Menu Bar > Tools > References).
首先,包括对Microsoft Scripting Runtime的参考(VB 编辑器菜单栏 > 工具 > 参考)。
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 Objectsare 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).
文件系统对象对于文件操作非常有用,尤其是在检查它们的存在和移动它们时。我喜欢使用它们早期绑定(Dim 语句),但如果您愿意,也可以使用它们晚期绑定(CreateObject 语句)。