如何从path中提取文件名?

如何在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 '\' ' eg '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 

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

创build一个文件系统对象,并使用它做所有的操作。

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

FileSystemObject是伟大的。 它提供了许多function,如获取特殊文件夹(我的文档等),以面向对象的方式创build,移动,复制,删除文件和目录。 一探究竟。

 Dir("C:\Documents\myfile.pdf") 

将返回文件名,但只有存在的话。

我已经读过所有的答案,我想补充一个,我认为胜出,因为它的简单性。 与接受的答案不同,这不需要recursion。 它也不需要引用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/有这个代码加上其他function分析出来的文件path,扩展名,甚至没有扩展名的文件名。;

 Dim sFilePath$, sFileName$ sFileName = Split(sFilePath, "\")(UBound(Split(sFilePath, "\"))) 

获取Excelmacros中的文件名是:

 filname = Mid(spth, InStrRev(spth, "\", Len(spth)) + 1, Len(spth)) MsgBox Mid(filname, 1, InStr(filname, ".") - 1) 

尝试使用splitfunction从path中获取文件名: MSDN链接

如果你想要一个更强大的解决scheme,将给你的完整文件夹的path和文件名,这里是:

 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 

或者作为一个子/function:

 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 

您将第一个parameter passing给文件的完整path,并将其设置为文件夹的path,而第二个参数将设置为该文件的名称。

这是我写的一个简单的VBA解决scheme,适用于Windows,Unix,Mac和URLpath。

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

您可以使用以下代码testing输出:

 '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 

另请参阅: 维基百科 – path(计算)

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

 Dim fileName, filePath As String filePath = "C:\Documents\myfile.pdf" fileName = Dir(filePath) 

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

 fileName = Mid(filePath, InStrRev(filePath, "\") + 1) 

这是一个没有代码的替代解决scheme。 此VBA在Excel配方栏中工作:

提取文件名称:

 =RIGHT(A1,LEN(A1)-FIND("~",SUBSTITUTE(A1,"\","~",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))) 

提取文件path:

 =MID(A1,1,LEN(A1)-LEN(MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"\",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))+1,LEN(A1)))) 
 Dim nme As String = My.Computer.FileSystem.GetFileInfo(pathFicheiro).Name Dim dirc As String = My.Computer.FileSystem.GetFileInfo(nomeFicheiro).Directory 

我需要的path,而不是文件名。

所以要在代码中提取文件path:

 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