|
其实Windows已经提供了相关的函数,下面的代码是通过API来实现的,仅供参考。因为在某些语言的系统中(如日文系统)路径分隔符并不是"\",所以没有使用字符来判断分割或是提取。
- #If VBA7 Then
- Private Declare PtrSafe Function PathRemoveFileSpec Lib "shlwapi.dll" Alias "PathRemoveFileSpecW" (ByVal pszPath As LongPtr) As Long
- Private Declare PtrSafe Function PathRemoveBackslashW Lib "shlwapi.dll" (ByVal pszPath As LongPtr) As Long
- Private Declare PtrSafe Function PathAddBackslashW Lib "shlwapi.dll" (ByVal pszPath As LongPtr) As Long
- Private Declare PtrSafe Function PathStripPath Lib "shlwapi.dll" Alias "PathStripPathW" (ByVal pszPath As LongPtr) As Long
- Private Declare PtrSafe Function PathRemoveExtension Lib "shlwapi.dll" Alias "PathRemoveExtensionW" (ByVal pszPath As LongPtr) As Long
- Private Declare PtrSafe Function PathFindExtension Lib "shlwapi.dll" Alias "PathFindExtensionW" (ByVal pszPath As LongPtr) As Long
- Private Declare PtrSafe Sub CopyMemoryString Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As LongPtr, ByVal Source As LongPtr, ByVal Length As Long)
- Private Declare PtrSafe Function lStrLen Lib "kernel32.dll" Alias "lstrlenW" (ByVal lpString As LongPtr) As Long
- #Else
- Private Declare Function PathRemoveFileSpec Lib "shlwapi.dll" Alias "PathRemoveFileSpecW" (ByVal pszPath As Long) As Long
- Private Declare Function PathRemoveBackslashW Lib "shlwapi.dll" (ByVal pszPath As Long) As Long
- Private Declare Function PathAddBackslashW Lib "shlwapi.dll" (ByVal pszPath As Long) As Long
- Private Declare Function PathStripPath Lib "shlwapi.dll" Alias "PathStripPathW" (ByVal pszPath As Long) As Long
- Private Declare Function PathRemoveExtension Lib "shlwapi.dll" Alias "PathRemoveExtensionW" (ByVal pszPath As Long) As Long
- Private Declare Function PathFindExtension Lib "shlwapi.dll" Alias "PathFindExtensionW" (ByVal pszPath As Long) As Long
- Private Declare Sub CopyMemoryString Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
- Private Declare Function lStrLen Lib "kernel32.dll" Alias "lstrlenW" (ByVal lpString As Long) As Long
- #End If
- '从路径中提取目录
- Public Function ExtractPathDirctory(ByVal sPath As String) As String
- Dim I As Long
-
- sPath = sPath & String(5, vbNullChar)
- PathRemoveBackslashW StrPtr(sPath)
- PathRemoveFileSpec StrPtr(sPath)
- PathAddBackslashW StrPtr(sPath)
- I = InStr(sPath, vbNullChar)
- If I > 0 Then sPath = Left$(sPath, I - 1)
- ExtractPathDirctory = sPath
- End Function
- '从路径中提取文件名,去掉了路径中的目录。
- Public Function ExtractFileName(ByVal sPath As String, Optional ByVal ExtensionReturn As Boolean = True) As String
- Dim I As Long, J As Long
- Dim strPath As String
-
- sPath = sPath & String(5, vbNullChar)
- PathRemoveBackslashW StrPtr(sPath)
- PathStripPath StrPtr(sPath)
- If Not ExtensionReturn Then PathRemoveExtension StrPtr(sPath)
- I = InStr(sPath, vbNullChar)
- If I > 0 Then sPath = Left$(sPath, I - 1)
- ExtractFileName = sPath
- End Function
- '从路径提取文件后缀名
- Public Function ExtractFileExtension(ByVal sPath As String) As String
- Dim ptrExt As Long
- Dim ExtLen As Long
-
- sPath = sPath & vbNullChar
- ptrExt = PathFindExtension(StrPtr(sPath))
- If ptrExt Then
- ExtLen = lStrLen(ptrExt)
- If ExtLen > 0 Then
- ExtractFileExtension = String(ExtLen, vbNullChar)
- CopyMemoryString StrPtr(ExtractFileExtension), ptrExt, LenB(ExtractFileExtension)
- End If
- End If
- End Function
- Sub Test()
- Const FileName As String = "C:\Windows.Old\1.TXT\ABC.123.后缀名有点怪"
- Debug.Print "文件全路径名:""" & FileName & """"
- Debug.Print "文件所在路径:""" & ExtractPathDirctory(FileName) & """"
- Debug.Print "带后缀文件名:""" & ExtractFileName(FileName) & """ 不带后缀文件名:""" & ExtractFileName(FileName, False) & """"
- Debug.Print "文件后缀名为:""" & ExtractFileExtension(FileName) & """"
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|