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很棒.它提供了许多功能,例如以面向对象的方式获取特殊文件夹(我的文档等),创建,移动,复制,删除文件和目录.看看这个.
Dic*_*ika 45
Dir("C:\Documents\myfile.pdf")
Run Code Online (Sandbox Code Playgroud)
将返回文件名,但仅在文件名存在时返回.
Gon*_*alo 40
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)
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/具有此代码以及其他功能,用于解析文件路径,扩展名甚至没有扩展名的文件名.
小智 8
Dim sFilePath$, sFileName$
sFileName = Split(sFilePath, "\")(UBound(Split(sFilePath, "\")))
Run Code Online (Sandbox Code Playgroud)
我不敢相信其中的一些答案过于复杂...(没有违法!)
这是一个单行函数,可以完成工作:

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

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)
如果您想要一个更强大的解决方案,该解决方案将为您提供完整文件夹的路径和文件名,这里是:
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)
您使用文件的完整路径传递第一个参数,它将被设置为文件夹的路径,而第二个参数将被设置为文件的名称。
这是我编写的一个简单的 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)
另请参阅:维基百科 - 路径(计算)
如果您确定该文件物理存在于磁盘上,最简单的方法是:
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)