技術(shù) 點(diǎn)
- 技術(shù)
- 點(diǎn)
- V幣
- 點(diǎn)
- 積分
- 1187
|
最近在學(xué)習(xí)VS2010的自動(dòng)化與擴(kuò)展.順手寫了幾個(gè)路徑信息的函數(shù)
'獲取文件名
Function GetFileName(Path As String) As String
Dim lPos As Long
lPos = InStrRev(Path, "\")
If lPos Then GetFileName = Mid$(Path, lPos + 1)
End Function
'獲取目錄名
Function GetDirName(Path As String) As String
Dim lPos As Long
lPos = InStrRev(Path, "\")
If lPos Then GetDirName = Mid$(Path, 1, lPos)
End Function
'獲取擴(kuò)展名
Function GetExtension(Path As String) As String
Dim lPos1 As Long, lPos2 As Long
lPos1 = InStrRev(Path, "\")
lPos2 = InStrRev(Path, ".")
'防止沒有擴(kuò)展名的文件
If lPos1 < lPos2 And lPos1 > 0 Then GetExtension = Mid$(Path, lPos2 + 1)
End Function
'獲取根目錄
Function GetPathRoot(Path As String) As String
Dim lPos As Long
If Left$(Path, 2) = "\\" Then '處理網(wǎng)絡(luò)路徑
lPos = InStr(3, Path, "\")
If lPos Then GetPathRoot = Mid$(Path, 1, lPos)
Else
lPos = InStr(1, Path, "\")
If lPos Then GetPathRoot = Left$(Path, lPos)
End If
End Function
'獲取文件名, 不帶擴(kuò)展名
Function GetFileNameNoExt(Path As String) As String
Dim lPos1 As Long, lPos2 As Long, strRet As String
lPos1 = InStrRev(Path, "\")
If lPos1 < 1 Then Exit Function
lPos2 = InStrRev(Path, ".")
If lPos1 < lPos2 Then '文件名存在擴(kuò)展名
GetFileNameNoExt = Mid$(Path, lPos1 + 1, lPos2 - lPos1 - 1)
Else '文件名沒有擴(kuò)展名
GetFileNameNoExt = Mid$(Path, lPos1 + 1)
End If
End Function
Sub Path_Test()
Dim strPath As String
strPath = "C:\dir1\dir2\foo.txt" '正常目錄
' strPath = "C:\dt01\dir.2\footxt" '沒有擴(kuò)展名
' strPath = "\\dt01\dir.2\footxt" '網(wǎng)絡(luò)路徑1
' strPath = "\\192.168.1.101\dir.2\foo.txt" '網(wǎng)絡(luò)路徑2
Debug.Print GetFileName(strPath)
Debug.Print GetDirName(strPath)
Debug.Print GetExtension(strPath)
Debug.Print GetPathRoot(strPath)
Debug.Print GetFileNameNoExt(strPath)
End Sub
有了上面的基本夠用了.
下面是整合的函數(shù)
|
|