【发布时间】:2009-11-16 16:39:21
【问题描述】:
如何在 VBA 中从 C:\Documents\myfile.pdf 中提取文件名 myfile.pdf?
【问题讨论】:
-
尝试使用split函数从路径获取文件名:MSDN link
如何在 VBA 中从 C:\Documents\myfile.pdf 中提取文件名 myfile.pdf?
【问题讨论】:
在 VBA for Office 2000/2003 中处理文件和目录的最佳方式是使用脚本库。
创建一个文件系统对象并使用它执行所有操作。
早期绑定:
添加对 Microsoft 脚本运行时的引用(IDE 中的工具 > 引用)。
Dim fso as new FileSystemObject
Dim fileName As String
fileName = fso.GetFileName("c:\any path\file.txt")
Late binding(更多信息请参见 cmets)
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);
Dir("C:\Documents\myfile.pdf")
将返回文件名,但前提是它存在。
【讨论】:
Dir 返回文件名,但前提是该文件实际存在于该位置。如果文件不存在,Dir 返回一个空字符串。如果您知道文件存在,例如当您提示用户选择文件时,那么这是获取文件名的快速简便的方法。如果您不知道该文件是否存在,来自@ArturPiwkowski 的Split 更好。两个Splits 将比任何字符串操作都快。
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 上,甚至返回旧的:-notation 中的路径。我必须编写一个函数来转换这些路径。
我已经阅读了所有答案,我想再添加一个我认为由于其简单性而胜出的答案。与公认的答案不同,这不需要递归。它也不需要引用 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() 的第三个参数是可选的。
Dim sFilePath$, sFileName$
sFileName = Split(sFilePath, "\")(UBound(Split(sFilePath, "\")))
【讨论】:
Split(sFilePath, "\")。
如果您想要一个更强大的解决方案,可以同时为您提供完整文件夹的路径和文件名,这里是:
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
您将第一个参数与文件的完整路径一起传递,它将被设置为文件夹的路径,而第二个参数将被设置为文件的名称。
【讨论】:
这是我编写的一个简单的 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
【讨论】:
在excel宏中获取文件名是:
filname = Mid(spth, InStrRev(spth, "\", Len(spth)) + 1, Len(spth))
MsgBox Mid(filname, 1, InStr(filname, ".") - 1)
【讨论】:
如果您确定文件实际存在于磁盘上,最简单的方法是:
Dim fileName, filePath As String
filePath = "C:\Documents\myfile.pdf"
fileName = Dir(filePath)
如果您不确定文件是否存在或只是想从给定路径中提取文件名,那么最简单的方法是:
fileName = Mid(filePath, InStrRev(filePath, "\") + 1)
【讨论】:
希望这会有所帮助。
【讨论】:
这是一个没有代码的替代解决方案。此 VBA 在 Excel 公式栏中工作:
提取文件名:
=RIGHT(A1,LEN(A1)-FIND("~",SUBSTITUTE(A1,"\","~",LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))))
提取文件路径:
=MID(A1,1,LEN(A1)-LEN(MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"\",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))+1,LEN(A1))))
【讨论】:
我正在使用这个功能... VBA 函数:
Function FunctionGetFileName(FullPath As String) As String
'Update 20140210
Dim splitList As Variant
splitList = VBA.Split(FullPath, "\")
FunctionGetFileName = splitList(UBound(splitList, 1))
End Function
现在输入
=FunctionGetFileName(A1) in youe required cell.
或者您可以使用这些...
=MID(A1,FIND("*",SUBSTITUTE(A1,"\","*",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))+1,LEN(A1))
【讨论】:
我需要的是路径,而不是文件名。
所以要提取代码中的文件路径:
JustPath = Left(sFileP, Len(sFileP) - Len(Split(sFileP, "\")(UBound(Split(sFileP, "\")))))
【讨论】:
这是从 Twiggy @http://archive.atomicmpc.com.au 和其他地方收集到的:
'since the file name and path were used several times in code
'variables were made public
Public FName As Variant, Filename As String, Path As String
Sub xxx()
...
If Not GetFileName = 1 Then Exit Sub '
...
End Sub
Private Function GetFileName()
GetFileName = 0 'used for error handling at call point in case user cancels
FName = Application.GetOpenFilename("Ramp log file (*.txt), *.txt")
If Not VarType(FName) = vbBoolean Then GetFileName = 1 'to assure selection was made
Filename = Split(FName, "\")(UBound(Split(FName, "\"))) 'results in file name
Path = Left(FName, InStrRev(FName, "\")) 'results in path
End Function
【讨论】:
Dim nme As String = My.Computer.FileSystem.GetFileInfo(pathFicheiro).Name
Dim dirc As String = My.Computer.FileSystem.GetFileInfo(nomeFicheiro).Directory