g9221712
高階會員
發表:145 回覆:344 積分:162 註冊:2006-07-06
發送簡訊給我
|
不知道怎麼轉換
- 取得檔名(傳回絕對<layer id="SiteStyleLine2LYR" top="35" left="14" visibility="INHERIT" width="606" height="7" z-index="2"></layer>
|
|
|
方法一 GetFullPathName 傳回一個絕對檔名,那 該Function的絕對長長路徑
呼叫例子:
Debug.Print toLongName("C:\PROGRA~1\ACCESS~1\MSPAINT.EXE")
Debug.Print toLongName("C:\PROGRAM FILES\ACCESS~1")
Debug.Print toLongName("..\MyFile")
Public Declare Function GetFullPathName Lib "kernel32" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As Long
Public Function toLongName(ByVal FileName As String) As String
Dim TokenStr As New Collection, pos As Integer
Dim FullPathName As String, I As Long
Dim ptstr As String
ptstr = String(256, 0)
FullPathName = String(256, 0)
'先取得 檔案/目錄 的完整目錄名稱
I = GetFullPathName(FileName, 256, FullPathName, ptstr)
FullPathName = Left(FullPathName, InStr(1, FullPathName, Chr(0)) - 1)
'如果該 檔案/目錄不存在則返回
If Len(Dir(FullPathName, vbDirectory vbNormal vbHidden vbSystem vbReadOnly)) = 0 Then
toLongName = ""
Exit Function
End If
'取得FullPtahName各個部份,如 C:\DIRECT~1\FILENAME
'將變成 C: DIRECT~1 FILENAME三個字串存在TokenStr
'的Collection中
Do While True
pos = InStr(1, FullPathName, "\")
If pos <> 0 Then
TokenStr.Add Left(FullPathName, pos - 1)
FullPathName = Mid(FullPathName, pos 1)
Else
TokenStr.Add FullPathName
Exit Do
End If
Loop
'取出各個Token,並以Dir指令取得 檔案/目錄 的檔名
toLongName = TokenStr(1) "\" '第一個一定是Driver名稱(如 c:)
Dim LongName As String
For I = 2 To TokenStr.Count
'Dir("C:\Progra~1") 會傳回 "Program Files"
LongName = Dir(toLongName TokenStr(I), vbNormal vbSystem vbArchive vbDirectory vbHidden)
toLongName = toLongName LongName "\"
Next
toLongName = Left(toLongName, Len(toLongName) - 1)
End Function
方法二
說明
程式
Public Function GetLongFileName(ByVal ShortFileName As String) As String
Dim intPos As Integer
Dim strLongFileName As String
Dim strDirName As String
'Format the filename for later processing
ShortFileName = ShortFileName & "\"
'Grab the position of the first real slash
intPos = InStr(4, ShortFileName, "\")
'Loop round all the directories and files
'in ShortFileName, grabbing the full names
'of everything within it.
While intPos
strDirName = Dir(Left(ShortFileName, intPos - 1), _
vbNormal vbHidden vbSystem vbDirectory)
If strDirName = "" Then
GetLongFileName = ""
Exit Function
End If
strLongFileName = strLongFileName & "\" & strDirName
intPos = InStr(intPos 1, ShortFileName, "\")
Wend
'Return the completed long file name
GetLongFileName = Left(ShortFileName, 2) & strLongFileName
End Function
相關資訊
文件出處
cww
VB Square
|
------ 「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
|
g9221712
高階會員
發表:145 回覆:344 積分:162 註冊:2006-07-06
發送簡訊給我
|
uses Windows, SysUtils;
function GetShortName(sLongName: string): string; var sShortName: string; nShortNameLen: Integer; begin SetLength(sShortName, MAX_PATH); nShortNameLen := GetShortPathName(PChar(sLongName), PChar(sShortName), MAX_PATH - 1); if (0 = nShortNameLen) then begin // handle errors... end; SetLength(sShortName, nShortNameLen); Result := sShortName; end;
------ 「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
|