2009-11-16 130 views

回答

36

这是从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 
+1

正确,但如果您在Mac上呢?不幸的是,这不是一个跨平台的解决方案.. – 2013-02-24 14:01:21

+3

只需用/替换代码中的\。 – Gonzalo 2013-02-25 05:40:17

+0

然后它在窗户上坏了吧?我已经想出了使用'Application.PathSeparator',但不幸的是,在OSX上,VBA甚至在旧的':'-notation中返回路径。我不得不写一个函数来转换这些路径。 – 2013-02-25 17:07:51

96

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

创建一个文件系统对象并使用它执行所有操作。

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

FileSystemObject很棒。它提供了很多功能,例如获取特殊文件夹(我的文档等),以面向对象的方式创建,移动,复制,删除文件和目录。一探究竟。

+11

这个答案胜过一英里接受的答案。 – 2014-12-29 08:28:15

+6

在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

+3

一句警告,如果您得到“用户定义类型未定义”错误,则需要设置对VB脚本运行时库的引用。 加载VB编辑器(ALT + F11),从下拉菜单中选择工具>引用,然后从可用引用列表中勾选“Microsoft Scripting Runtime”旁边的复选框并单击确定。 – 2015-11-06 12:21:34

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

将返回文件名,但仅当它存在时才返回。

+0

+1“很短” – golimar 2014-11-13 10:07:41

+3

不好,因为它假设路径是用于程序可以读取的物理文件。 – Skrol29 2015-11-06 15:41:33

+0

嗨@Skrol29,你能帮忙解释一下吗? – 2015-12-17 00:21:00

3

要在Excel宏获得的文件名是:

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

如果你想有一个更强大的解决方案,这将使你俩完整的文件夹的路径和文件名,在这里它是:

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 

您传递的完整路径的第一个参数并将其设置为文件夹的路径,而第二个参数将设置为文件的名称。

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

不那么聪明,因为它运行了两次'Split(sFilePath,“\”)'。 – Skrol29 2015-11-06 15:39:14

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)))) 
0

我所需要的路径,而不是文件名。

所以要提取代码的文件路径:

JustPath = Left(sFileP, Len(sFileP) - Len(Split(sFileP, "\")(UBound(Split(sFileP, "\"))))) 
21

我已经通过所有的答案阅读,我想补充一个,我认为赢得因为它的简单了。与公认的答案不同,这不需要递归。它也不需要引用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/有这个代码以及其他功能解析出的文件路径,扩展名,甚至文件名不带扩展名。

+1

对我来说最好的:聪明,矮,并且越野。 – Skrol29 2015-11-06 15:51:33

+2

+1为简单起见 - 甚至可以放弃该功能,并按原样使用“=”行,或者在一个循环中使用多个文件。 – tahwos 2016-10-03 03:45:35

+0

我比其他答案更喜欢这个。它很容易在任何其他子功能或函数中重复使用,不需要任何参考摆弄 – mattlore 2017-09-13 17:53:39

2

这是我写的一个简单的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 

另见:Wikipedia - Path (computing)

0

这从纤细收集@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 
1
Dim nme As String = My.Computer.FileSystem.GetFileInfo(pathFicheiro).Name 
Dim dirc As String = My.Computer.FileSystem.GetFileInfo(nomeFicheiro).Directory 
+0

尽管您的答案可能适用于该问题,请您介绍一下正在做什么? – Ivan 2015-08-25 12:00:40

+0

这是更好的解决方案。它从给定路径pathFicheiro中提取文件名和目录。没有必要使用nomeFicheiro,因为你可以使用pathFicheiro。 pathFicheiro = FILENAME变量 – user1054844 2016-11-15 09:25:34

2

如果您确定该文件在磁盘上实际存在的最简单的方法:

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

如果你不能确定文件是否存在,或者只是想提取一个给定的路径文件名的话,最简单的方法是:

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