設(shè)為首頁收藏本站Access中國

Office中國論壇/Access中國論壇

 找回密碼
 注冊

QQ登錄

只需一步,快速開始

123下一頁
返回列表 發(fā)新帖
查看: 6136|回復(fù): 25
打印 上一主題 下一主題

[模塊/函數(shù)] [原創(chuàng)][分享]獲取路徑信息函數(shù)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
1#
發(fā)表于 2015-11-22 09:07:19 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
最近在學(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ù)
游客,如果您要查看本帖隱藏內(nèi)容請回復(fù)


本帖被以下淘專輯推薦:

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享分享 分享淘帖1 訂閱訂閱

點(diǎn)擊這里給我發(fā)消息

2#
發(fā)表于 2015-11-22 09:19:18 來自手機(jī) | 只看該作者
wang1999 發(fā)表于 2015-11-22 09:07
最近在學(xué)習(xí)VS2010的自動(dòng)化與擴(kuò)展.順手寫了幾個(gè)路徑信息的函數(shù)
'獲取文件名
Function GetFileName(Path As ...

贊一個(gè)
來自: 微社區(qū)
3#
發(fā)表于 2015-11-22 13:26:51 | 只看該作者
謝謝分享!
回復(fù)

使用道具 舉報(bào)

4#
發(fā)表于 2015-11-23 00:27:25 | 只看該作者
eeee
回復(fù)

使用道具 舉報(bào)

5#
發(fā)表于 2015-11-23 07:00:48 來自手機(jī) | 只看該作者
好東西來自: Android客戶端
回復(fù)

使用道具 舉報(bào)

6#
發(fā)表于 2015-11-23 07:01:58 來自手機(jī) | 只看該作者
要是天有真好!來自: Android客戶端
7#
發(fā)表于 2015-11-23 08:55:32 | 只看該作者
挺好的。
回復(fù)

使用道具 舉報(bào)

8#
發(fā)表于 2015-11-23 09:27:05 | 只看該作者
謝謝分享
回復(fù)

使用道具 舉報(bào)

點(diǎn)擊這里給我發(fā)消息

9#
發(fā)表于 2015-11-23 12:00:42 | 只看該作者
贊一個(gè){:soso_e179:}

點(diǎn)擊這里給我發(fā)消息

10#
發(fā)表于 2015-11-23 13:17:40 | 只看該作者
贊一個(gè)
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則

QQ|站長郵箱|小黑屋|手機(jī)版|Office中國/Access中國 ( 粵ICP備10043721號-1 )  

GMT+8, 2024-10-23 08:36 , Processed in 0.106913 second(s), 35 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回復(fù) 返回頂部 返回列表