Private Sub Command1_Click() Dim st() As String Str1 = "c:\OAProgbc file\backfile2004 09 09\master 20040909\oa.exe" st = Split(Str1, "\") For i = 1 To UBound(st) If Len(st(i)) > 8 Then st(i) = Left(st(i), 6) + "~1" End If Next str2 = Join(st, "\") Print str2 End Sub
Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long Private Sub Command1_Click() Dim str1 As String, str2 As String str1 = "C:\Documents and Settings\All Users\Application Data" str2 = Space(256) GetShortPathName str1, str2, 256 str2 = StrConv(Left(str2, InStr(str2, " ")), vbNarrow) Debug.Print str2 End Sub C:\DOCUME~1\ALLUSE~1\APPLIC~1
上面不要转换 str2 = Left(str2, InStr(str2, " "))
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As LongPrivate Sub Command1_Click() Dim PathName As String, S As String, ShortPathName As String PathName = "C:\WINDOWS\Media\Windows XP 默认值.wav" S = String(LenB(PathName), Chr(0)) GetShortPathName PathName, S, Len(S) ShortPathName = Left(S, InStr(S, Chr(0)) - 1) End Sub
或者用FSO工程-引用 Microsoft Scripting RuntimePrivate Sub Form_Load() Dim fso As FileSystemObject Dim fsoFile As File Set fso = New FileSystemObject Set fsoFile = fso.GetFile(长文件名) MsgBox fsoFile.ShortPath Set fsoFile = Nothing Set fso = Nothing End Sub
Dim st() As String
Str1 = "c:\OAProgbc file\backfile2004 09 09\master 20040909\oa.exe"
st = Split(Str1, "\")
For i = 1 To UBound(st)
If Len(st(i)) > 8 Then
st(i) = Left(st(i), 6) + "~1"
End If
Next
str2 = Join(st, "\")
Print str2
End Sub
Long,装载到lpszShortPath缓冲区的字符数量。如lpszShortPath的长度不足,不能容下文件名,就返回需要的缓冲区长度
参数
lpszLongPath --- String,指定欲获取短路径名的那个文件的名字。可以是个完整路径,或者由当前目录决定
lpszShortPath -- String,指定一个缓冲区,用于装载文件的短路径和文件名
cchBuffer ------ Long,lpszShortPath缓冲区长度
Private Sub Command1_Click()
Dim str1 As String, str2 As String
str1 = "C:\Documents and Settings\All Users\Application Data"
str2 = Space(256)
GetShortPathName str1, str2, 256
str2 = StrConv(Left(str2, InStr(str2, " ")), vbNarrow)
Debug.Print str2
End Sub
C:\DOCUME~1\ALLUSE~1\APPLIC~1
str2 = Left(str2, InStr(str2, " "))
Dim PathName As String, S As String, ShortPathName As String
PathName = "C:\WINDOWS\Media\Windows XP 默认值.wav"
S = String(LenB(PathName), Chr(0))
GetShortPathName PathName, S, Len(S)
ShortPathName = Left(S, InStr(S, Chr(0)) - 1)
End Sub
Microsoft Scripting RuntimePrivate Sub Form_Load()
Dim fso As FileSystemObject
Dim fsoFile As File Set fso = New FileSystemObject
Set fsoFile = fso.GetFile(长文件名)
MsgBox fsoFile.ShortPath Set fsoFile = Nothing
Set fso = Nothing
End Sub