如何在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. 如何僅從路徑中提取文件/文件夾名稱?
- 5. C# - 如何從路徑中提取文件名和擴展名?
- 6. 如何提取路徑的文件名
- 7. 從文件名中提取文件名,路徑來自參數
- 8. 如何從路徑中提取文件名
- 9. 如何從其路徑中提取文件名vba
- 10. 如何從路徑中提取每個文件夾的名稱?
- 11. 如何從路徑/字符串中提取文件名?
- 12. 如何從OpenFileDialog中提取文件的路徑名?
- 13. 如何從蝙蝠的完整路徑中提取文件名?
- 14. 如何從使用Perl的路徑中提取文件名?
- 15. 如何從路徑獲取文件名?
- 16. Python - 從文件路徑中提取文件夾路徑
- 17. 如何從文本文件中提取文件路徑
- 18. 從C#文件路徑中提取文件名?
- 19. 提取文件名從在csh shell路徑 - 從文件
- 20. 從文件路徑獲取文件名
- 21. 如何從ls中獲取全名,路徑和文件名?
- 22. UDF從Spark SQL中的路徑中僅提取文件名
- 23. 從分組文件路徑集合中提取公共路徑?
- 24. 如何使用Apache Ant從文件路徑中提取軟件包名稱?
- 25. VBA - 從路徑中提取特定的文件夾名稱
- 26. 從目錄路徑中提取文件名
- 27. 使用perl從路徑中提取文件名(IE8瀏覽器)
- 28. 從AppleScript路徑中提取文件擴展名
- 29. 正則表達式從URL中提取路徑+文件名
- 30. 正則表達式從Perforce的路徑中提取文件名
正確,但如果您在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