如何在VBA中从C:\Documents\myfile.pdf
提取文件名myfile.pdf
?如何从路径中提取文件名?
回答
这是从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
尝试使用分裂函数来获取从路径的文件名: MSDN link
在VBA文件和目录工作的最好办法Office 2000/2003正在使用脚本库。添加对Microsoft脚本运行时的引用(IDE中的工具>引用)。
创建一个文件系统对象并使用它执行所有操作。
Dim fso as new FileSystemObject
Dim fileName As String
fileName = fso.GetFileName("c:\any path\file.txt")
FileSystemObject很棒。它提供了很多功能,例如获取特殊文件夹(我的文档等),以面向对象的方式创建,移动,复制,删除文件和目录。一探究竟。
这个答案胜过一英里接受的答案。 – 2014-12-29 08:28:15
在Excel VBA中,'Dim fileName As String; Dim fso as Object;设置fso = CreateObject(“Scripting.FileSystemObject”); fileName = fso.GetFilename(path);' – IceArdor 2015-04-27 23:46:44
一句警告,如果您得到“用户定义类型未定义”错误,则需要设置对VB脚本运行时库的引用。 加载VB编辑器(ALT + F11),从下拉菜单中选择工具>引用,然后从可用引用列表中勾选“Microsoft Scripting Runtime”旁边的复选框并单击确定。 – 2015-11-06 12:21:34
要在Excel宏获得的文件名是:
filname = Mid(spth, InStrRev(spth, "\", Len(spth)) + 1, Len(spth))
MsgBox Mid(filname, 1, InStr(filname, ".") - 1)
如果你想有一个更强大的解决方案,这将使你俩完整的文件夹的路径和文件名,在这里它是:
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 sFilePath$, sFileName$
sFileName = Split(sFilePath, "\")(UBound(Split(sFilePath, "\")))
不那么聪明,因为它运行了两次'Split(sFilePath,“\”)'。 – Skrol29 2015-11-06 15:39:14
这是一个没有代码的替代解决方案。这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))))
我所需要的路径,而不是文件名。
所以要提取代码的文件路径:
JustPath = Left(sFileP, Len(sFileP) - Len(Split(sFileP, "\")(UBound(Split(sFileP, "\")))))
我已经通过所有的答案阅读,我想补充一个,我认为赢得因为它的简单了。与公认的答案不同,这不需要递归。它也不需要引用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/有这个代码以及其他功能解析出的文件路径,扩展名,甚至文件名不带扩展名。
这是我写的一个简单的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
这从纤细收集@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
尽管您的答案可能适用于该问题,请您介绍一下正在做什么? – Ivan 2015-08-25 12:00:40
这是更好的解决方案。它从给定路径pathFicheiro中提取文件名和目录。没有必要使用nomeFicheiro,因为你可以使用pathFicheiro。 pathFicheiro = FILENAME变量 – user1054844 2016-11-15 09:25:34
如果您确定该文件在磁盘上实际存在的最简单的方法:
Dim fileName, filePath As String
filePath = "C:\Documents\myfile.pdf"
fileName = Dir(filePath)
如果你不能确定文件是否存在,或者只是想提取一个给定的路径文件名的话,最简单的方法是:
fileName = Mid(filePath, InStrRev(filePath, "\") + 1)
- 1. 如何从文件路径名中提取文件名?
- 2. 如何仅从路径中提取文件/文件夹名称?
- 3. 如何从文件路径中提取文件名?
- 4. 如何从OpenFileDialog中提取文件的路径名?
- 5. 如何从使用Perl的路径中提取文件名?
- 6. 如何从路径获取文件名?
- 7. 如何从文本文件中提取文件路径
- 8. 提取文件名从在csh shell路径 - 从文件
- 9. 从文件路径获取文件名
- 10. UDF从Spark SQL中的路径中仅提取文件名
- 11. 从分组文件路径集合中提取公共路径?
- 12. 如何使用Apache Ant从文件路径中提取软件包名称?
- 13. 正则表达式从URL中提取路径+文件名
- 14. VBA - 从路径中提取特定的文件夹名称
- 15. 从目录路径中提取文件名
- 16. 从变量中批量提取路径和文件名
- 17. 如何从文件名获取文件路径和目录?
- 18. 如何从文件路径获取父文件夹的名称?
- 19. 如何从路径中提取标签?
- 20. 如何从Perl字符串中提取文件路径?
- 21. 从路径获取文件夹名称
- 22. 从JavaScript路径获取文件名
- 23. 从路径获取文件名
- 24. 从完整路径获取文件名
- 25. 如何从路径中获取文件名的词干?
- 26. 如何从路径中获取文件名
- 27. 如何通过提供文件名来获取完整路径?
- 28. 提取路径到目录从路径到文件
- 29. 从路径中提取点
- 30. 如何从URI获取文件路径?
正确,但如果您在Mac上呢?不幸的是,这不是一个跨平台的解决方案.. – 2013-02-24 14:01:21
只需用/替换代码中的\。 – Gonzalo 2013-02-25 05:40:17
然后它在窗户上坏了吧?我已经想出了使用'Application.PathSeparator',但不幸的是,在OSX上,VBA甚至在旧的':'-notation中返回路径。我不得不写一个函数来转换这些路径。 – 2013-02-25 17:07:51