如何在VBA中从 C:\Documents\myfile.pdf
中提取文件名myfile.pdf
?
VBA在Office 2000/2003中处理文件和目录最好的方法是使用脚本库。
创建一个文件系统对象,并使用它来进行所有操作。
早期绑定:
在IDE中添加对Microsoft Scripting Runtime的引用(工具 > 引用)。
Dim fso as new FileSystemObject
Dim fileName As String
fileName = fso.GetFileName("c:\any path\file.txt")
Late binding(有关注释请参考)
With CreateObject("Scripting.FileSystemObject")
fileName = .GetFileName(FilePath)
extName = .GetExtensionName(FilePath)
baseName = .GetBaseName(FilePath)
parentName = .GetParentFolderName(FilePath)
End With
FileSystemObject
非常好用。它提供了许多功能,例如以面向对象的方式获取特殊文件夹(我的文档等),创建、移动、复制、删除文件和目录。
Dim fileName As String; Dim fso as Object; Set fso = CreateObject("Scripting.FileSystemObject"); fileName = fso.GetFilename(path);
的意思是声明一个字符串类型的变量fileName和一个Object类型的变量fso,然后使用CreateObject方法创建一个Scripting.FileSystemObject对象并将其分配给fso变量。最后,通过调用fso.GetFilename(path)方法来获取指定路径下文件的文件名并将其分配给fileName变量。 - IceArdorDir("C:\Documents\myfile.pdf")
如果文件存在,将返回文件名。
Dir
返回文件名,但仅当该位置实际存在该文件时。如果文件不存在,则 Dir
返回一个空字符串。如果您知道文件存在,例如当您提示用户选择文件时,则这是获取文件名的快速简便方法。如果您不知道文件是否存在,则来自 @ArturPiwkowski 的 Split
更好。两个 Splits
将比任何字符串操作更快。 - Dick Kusleika这段内容来自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
Application.PathSeparator
,但不幸的是,在OSX VBA中甚至返回旧的:
表示法的路径。我不得不编写一个将这些路径转换的函数。 - Thomas FankhauserApplication.PathSeparator
具有错误的值,并且VBA跨平台运行,则具有OS感知的Application.PathSeparator
没有意义。 - Thomas Fankhauser我已经阅读了所有的回答,并且我想添加一个因其简单性而胜出的答案。与被接受的答案不同,这不需要递归。它也不需要引用FileSystemObject。
Function FileNameFromPath(strFullPath As String) As String
FileNameFromPath = Right(strFullPath, Len(strFullPath) - InStrRev(strFullPath, "\"))
End Function
http://vba-tutorial.com/parsing-a-file-string-into-path-filename-and-extension/ 这个网站有这段代码,以及其他用于解析文件路径、扩展名甚至是不带扩展名的文件名的函数。
Left(pf,InStrRev(pf,"\"))
。这利用了 mid()
的第三个参数是可选的。 - BenDim sFilePath$, sFileName$
sFileName = Split(sFilePath, "\")(UBound(Split(sFilePath, "\")))
Split(sFilePath, "\")
。 - Skrol29filname = Mid(spth, InStrRev(spth, "\", Len(spth)) + 1, Len(spth))
MsgBox Mid(filname, 1, InStr(filname, ".") - 1)
这里是我编写的一个简单的VBA解决方案,可用于Windows、Unix、Mac和URL路径。
sFileName = Mid(Mid(sPath, InStrRev(sPath, "/") + 1), InStrRev(sPath, "\") + 1)
sFolderName = Left(sPath, Len(sPath) - Len(sFileName))
'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
另外参考:维基百科 - 路径(计算机)
该链接介绍了路径的更多信息。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
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
您需要将文件的完整路径作为第一个参数传递,它将被设置为文件夹的路径,而第二个参数将被设置为文件名。
Dim fileName, filePath As String
filePath = "C:\Documents\myfile.pdf"
fileName = Dir(filePath)
如果您不确定文件是否存在,或者只想从给定路径中提取文件名,则最简单的方法是:
fileName = Mid(filePath, InStrRev(filePath, "\") + 1)