如何
从以下完整路径中
获取
像“MyFileName”这样
的文件名(没有路径和扩展名)?
C:\A_B\CD\E_\F0123456789\G\MyFileName.txt
问问题
9104 次
5 回答
2
InStrRev
将查找字符串中最后一次出现的字符。在那里搜索\
并拆分它
FullFileName="C:\A_B\C.D\E_\F0123456789\G\MyFileName.txt"
FileName=mid(FullFileName,instrrev(FullFileName,"\")+1)
现在取消扩展
FileNameWithoutExt=left(FileName,instrrev(FileName,".")-1)
于 2012-09-10T15:29:23.053 回答
2
Public Function GetFileNameWithoutExt(ByVal fullPath As String) As String
Dim fileName As String
Dim fileNameWithoutExt As String
Dim lastSlash As Integer
Dim positionOfDot As Integer
lastSlash = InStrRev(fullPath, "\")
fileName = Mid(fullPath, lastSlash + 1)
positionOfDot = InStr(1, fileName, ".")
fileNameWithoutExt = Mid(fileName, 1, positionOfDot - 1)
GetFileNameWithoutExt = fileNameWithoutExt
End Function
使用即时窗口
?GetFileNameWithoutExt("C:\A_B\C.D\E_\F0123456789\G\MyFileName.txt")
编辑:另一种方法
Public Function GetFileNameWithoutExt2(ByVal fullPath As String) As String
Dim fileName As String
Dim splittedData
Dim fileNameWithoutExt As String
splittedData = Split(fullPath, "\")
fileName = splittedData(UBound(splittedData))
fileNameWithoutExt = Split(fileName, ".")(0)
GetFileNameWithoutExt2 = fileNameWithoutExt
End Function
于 2012-09-10T15:30:52.517 回答
1
If it's a real file that you have access to, you can use Dir
sFileOnly = Dir(sPathAndFile)
If it's not a real file or you don't have access to it, this will return an empty string.
于 2012-09-10T18:30:23.700 回答
0
Sub Test()
Dim fileNameOnly As String
fileNameOnly = Left$(Split("C:\A_B\C.D\E_\F0123456789\G\MyFileName.txt", "\")(UBound(Split("C:\A_B\C.D\E_\F0123456789\G\MyFileName.txt", "\"))), InStrRev(Split("C:\A_B\C.D\E_\F0123456789\G\MyFileName.txt", "\")(UBound(Split("C:\A_B\C.D\E_\F0123456789\G\MyFileName.txt", "\"))), ".") - 1)
Debug.Print Strtf
End Sub
于 2012-09-10T15:39:37.840 回答
0
Set regEx = New RegExp
regEx.Pattern = ".*\\"
regEx.IgnoreCase = True
filename = regEx.Replace(fullpath, "")
于 2012-09-10T15:22:09.777 回答