如何从路径中提取文件名?

Joh*_*han 53 string vba

如何myfile.pdfC:\Documents\myfile.pdfVBA中提取文件名?

Zen*_*Zen 126

在VBA for Office 2000/2003中使用文件和目录的最佳方法是使用脚本库.添加对Microsoft Scripting Runtime的引用(IDE中的"工具">"引用").

创建一个文件系统对象并使用它执行所有操作.

Dim fso as new FileSystemObject
Dim fileName As String
fileName = fso.GetFileName("c:\any path\file.txt")
Run Code Online (Sandbox Code Playgroud)

FileSystemObject很棒.它提供了许多功能,例如以面向对象的方式获取特殊文件夹(我的文档等),创建,移动,复制,删除文件和目录.看看这个.

  • 这个答案比一英里接受的答案更胜一筹. (18认同)
  • 在Excel VBA中,`Dim fileName As String; 昏暗的fso作为对象; 设置fso = CreateObject("Scripting.FileSystemObject"); fileName = fso.GetFilename(path);` (8认同)
  • @IceArdor对Scripting Runtime进行后期绑定只能为您买到任何东西.在运行时解析调用会产生性能损失,零编译时检查意味着你输了一个错误,直到你运行代码才能知道它,这将很乐意编译.自Win98以来,不要在每台Windows机器上后期绑定标准交付的库. (4认同)
  • 一句警告,如果您收到"用户定义的类型未定义"错误,则需要设置对VB脚本运行时库的引用.加载VB编辑器(ALT + F11),从下拉菜单中选择工具>引用,并从可用引用列表中选中"Microsoft Scripting Runtime"旁边的复选框,然后单击"确定". (3认同)
  • 不是一个好的解决方案:它需要对 VBA 的依赖。该代码不适用于所有 VBA 环境。 (2认同)
  • @ Skrol29世界上每个Windows框都包含该库.没什么好担心的.接受的答案也无法在Mac上运行.系统地延迟绑定Scripting运行时的人不知道他们在做什么,也不知道为什么.这个答案是完全可以接受的,并且*确实*击败了接受的答案一英里.或者两个. (2认同)

Dic*_*ika 45

Dir("C:\Documents\myfile.pdf")
Run Code Online (Sandbox Code Playgroud)

将返回文件名,但仅在文件名存在时返回.

  • 不好,因为它假定路径是针对程序可以读取的物理文件. (3认同)

Gon*_*alo 40

这取自snippets.dzone.com:

Function GetFilenameFromPath(ByVal strPath As String) As String
' Returns the rightmost characters of a string upto but not including the rightmost '\'
' e.g. 'c:\winnt\win.ini' returns 'win.ini'

    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function
Run Code Online (Sandbox Code Playgroud)

  • 这是有效的,但是不必要地递归,特别是对于VBA(它不会自动将尾递归转换为循环). (4认同)
  • 恕我直言,http:http://stackoverflow.com/a/1755577/481207是一个更好的答案.代码重用,没有递归,没有嵌入常量. (4认同)

use*_*436 27

我已经阅读了所有的答案,我想补充一点,因为它的简单性我认为胜出.与接受的答案不同,这不需要递归.它也不需要引用FileSystemObject.

Function FileNameFromPath(strFullPath As String) As String

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

End Function
Run Code Online (Sandbox Code Playgroud)

http://vba-tutorial.com/parsing-a-file-string-into-path-filename-and-extension/具有此代码以及其他功能,用于解析文件路径,扩展名甚至没有扩展名的文件名.

  • 对我来说最好的:聪明,短,越野。 (3认同)
  • 为简单起见+1 - 甚至可以放弃该功能,并按原样使用"="行,或在循环中使用多个文件. (3认同)
  • 与其他任何答案相比,我更喜欢这个。它很容易在任何其他子或函数中重用,并且不需要对引用进行任何摆弄 (2认同)

小智 8

Dim sFilePath$, sFileName$
sFileName = Split(sFilePath, "\")(UBound(Split(sFilePath, "\")))
Run Code Online (Sandbox Code Playgroud)


ash*_*awg 6

我不敢相信其中的一些答案过于复杂...(没有违法!)

这是一个单行函数,可以完成工作:


**从<code> x:\ path \ filename </ code>:**中提取文件名

Function getFName(pf)As String:getFName=Mid(pf,InStrRev(pf,"\")+1):End Function
Run Code Online (Sandbox Code Playgroud)

**从<code> x:\ path \ filename </ code>:**中提取路径

Function getPath(pf)As String:getPath=Left(pf,InStrRev(pf,"\")):End Function
Run Code Online (Sandbox Code Playgroud)

例子:

例子


小智 5

要在Excel宏中获取文件名是:

filname = Mid(spth, InStrRev(spth, "\", Len(spth)) + 1, Len(spth))
MsgBox Mid(filname, 1, InStr(filname, ".") - 1)
Run Code Online (Sandbox Code Playgroud)


dnL*_*nLL 5

如果您想要一个更强大的解决方案,该解决方案将为您提供完整文件夹的路径和文件名,这里是:

Dim strFileName As String, strFolderPath As String
Dim lngIndex As Long
Dim strPath() As String

strPath() = Split(OpenArgs, "\")   'Put the Parts of our path into an array
lngIndex = UBound(strPath)
strFileName = strPath(lngIndex)    'Get the File Name from our array
strPath(lngIndex) = ""             'Remove the File Name from our array
strFolderPath = Join(strPath, "\") 'Rebuild our path from our array
Run Code Online (Sandbox Code Playgroud)

或作为子/功能:

Private Sub SeparatePathAndFile(ByRef io_strFolderPath As String, ByRef o_strFileName As String)    
    Dim strPath() As String
    Dim lngIndex As Long

    strPath() = Split(io_strFolderPath, "\")  'Put the Parts of our path into an array
    lngIndex = UBound(strPath)
    o_strFileName = strPath(lngIndex)   'Get the File Name from our array
    strPath(lngIndex) = ""              'Remove the File Name from our array
    io_strFolderPath = Join(strPath, "\")     'Rebuild our path from our array  
End Sub
Run Code Online (Sandbox Code Playgroud)

您使用文件的完整路径传递第一个参数,它将被设置为文件夹的路径,而第二个参数将被设置为文件的名称。


Rob*_*rto 5

这是我编写的一个简单的 VBA 解决方案,它适用于 Windows、Unix、Mac 和 URL 路径。

sFileName = Mid(Mid(sPath, InStrRev(sPath, "/") + 1), InStrRev(sPath, "\") + 1)

sFolderName = Left(sPath, Len(sPath) - Len(sFileName))
Run Code Online (Sandbox Code Playgroud)

您可以使用以下代码测试输出:

'Visual Basic for Applications 
http = "https://www.server.com/docs/Letter.txt"
unix = "/home/user/docs/Letter.txt"
dos = "C:\user\docs\Letter.txt"
win = "\\Server01\user\docs\Letter.txt"
blank = ""

sPath = unix 
sFileName = Mid(Mid(sPath, InStrRev(sPath, "/") + 1), InStrRev(sPath, "\") + 1)
sFolderName = Left(sPath, Len(sPath) - Len(sFileName))

Debug.print "Folder: " & sFolderName & " File: " & sFileName
Run Code Online (Sandbox Code Playgroud)

另请参阅:维基百科 - 路径(计算)


ePa*_*dit 5

如果您确定该文件物理存在于磁盘上,最简单的方法是:

Dim fileName, filePath As String
filePath = "C:\Documents\myfile.pdf"
fileName = Dir(filePath)
Run Code Online (Sandbox Code Playgroud)

如果您不确定文件是否存在或只想从给定路径中提取文件名,那么最简单的方法是:

fileName = Mid(filePath, InStrRev(filePath, "\") + 1)
Run Code Online (Sandbox Code Playgroud)