您的位置:寻梦网首页编程乐园VB 编程乐园VB问题全功略

VB问题全功略(57

上一页(57下一页

281、如何从全路径名中提取文件的扩展名?
282、如何从全路径名中提取文件名(从前向后)?
283、如何翻转一个字符串?
284、如何分离出路径和文件名?
285、如何将长的目录名缩短?

281、如何从全路径名中提取文件的扩展名?

Option Explicit
Public Function GetExtension(Filename As String)
Dim i, j, PthPos, ExtPos As Integer
For i = Len(Filename) To 1 Step -1 ' Go from the Length of the filename, to the first character by 1.
If Mid(Filename, i, 1) = "." Then ' If the current position is '.' then...
ExtPos = i ' ...Change the ExtPos to the number.
For j = Len(Filename) To 1 Step -1 ' Do the Same...
If Mid(Filename, j, 1) = "\" Then ' ...but for '\'.
PthPos = j ' Change the PthPos to the number.
Exit For ' Since we found it, don't search any more.
End If
Next j
Exit For ' Since we found it, don't search any more.
End If
Next i
If PthPos > ExtPos Then
Exit Function ' No extension.
Else
If ExtPos = 0 Then Exit Function ' If there is not extension, then exit sub.
GetExtension = Mid(Filename, ExtPos + 1, Len(Filename) - ExtPos) 'Messagebox the Extension
End If
End Function
'使用:
'FileExt = GetExtension("c:\windows\vb\vb.exe")

282、如何从全路径名中提取文件名(从前向后)?

Option Explicit
Function StripPath(T$) As String
Dim x%, ct%
StripPath$ = T$
x% = InStr(T$, "\")
Do While x%
ct% = x%
x% = InStr(ct% + 1, T$, "\")
Loop
If ct% > 0 Then StripPath$ = Mid$(T$, ct% + 1)
End Function
'例子:
'File = StripPath("c:\windows\hello.txt")

283、如何翻转一个字符串?

翻转一个字符串
下面的函数利用递归原理获得字符串的翻转字符串
Function reversestring(revstr As String) As String
' revstr: 要翻转的字符串
' 返回值:翻转后的字符串
Dim doreverse As Long
reversestring = ""
For doreverse = Len(revstr) To 1 Step -1
reversestring = reversestring & Mid$(revstr, doreverse, 1)
Next
End Function

284、如何分离出路径和文件名?

Public Function GETPATH(ByVal PATHANDNAME As String, Optional filename As String) As String
'把带有含有文件和路径的字符串分为路径和文件两个字符串输出.GETPATH 返回路径,filename 返回文件名.
'get path and filename separated. no "\" at the end of path after.
'author NorthWest Donkey nwdonkey@371.net
For i = Len(PATHANDNAME) To 1 Step -1
slash = Mid(PATHANDNAME, i, 1)
If slash = "\" Then Exit For
Next i
If i <> 0 Then
filename = Mid(PATHANDNAME, i + 1, Len(PATHANDNAME) - i)
GETPATH = Left(PATHANDNAME, i - 1)
End If
End Function

285、如何将长的目录名缩短?

Public Function path2long(ByVal LongPath As String, ByVal reduce2 As Integer) As String
'将长的目录名缩短
'如:由 "C:\Program Files\Vb5\我的最新程序库\temp" 变成 "...\Vb5\我的最新程序库\temp"
Dim i As Integer
Dim slash As String
If reduce2 < Len(LongPath) Then
path2long = Right(LongPath, reduce2 - 3) 'get rid of extensions
For i = 1 To Len(path2long)
slash = Mid(path2long, i, 1)
If slash = "\" Then Exit For
Next i
If i <> 0 Then
path2long = "..." & Right(path2long, Len(path2long) - i + 1)
End If
Else
path2long = LongPath
End If
End Function

上一页(57下一页