2009-11-16 58 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) 
相關問題