先看看怎样应用:
'提取文件名全称(完整路径 As String)
'提取扩展名(文件名全称 As String)
'获取字符串字节数(字符串 As String)
'获取文件名称(文件名全称 As String)
’===================窗体代码=====================
Private Sub Command20_Click()
Text1.Text = "从全路径名中提取文件名全称: " & 提取文件名全称("C:\AAA\BBB\CCC.exe")
Text2.Text = "从文件名全称中获取文件名(ABC.exe): " & 获取文件名称("ABC.exe")
Text3.Text = "提取扩展名(CCC.exe): " & 提取扩展名("CCC.exe")
Text4.Text = "获取字符串字节数(“钟点工”): " & 获取字符串字节数("钟点工")
End Sub
’===================模块代码=====================
Function 提取文件名全称(完整路径 As String)
Dim X%, ct%
X% = InStr(完整路径, "\")
Do While X%
ct% = X%
X% = InStr(ct% + 1, 完整路径, "\")
Loop
If ct% > 0 Then 提取文件名全称 = Mid(完整路径, ct% + 1)
End Function
Public Function 提取扩展名(文件名全称 As String)
Dim I, J, PthPos, ExtPos As Integer
For I = Len(文件名全称) To 1 Step -1 ' Go from the Length of the 文件名全称, to the first character by 1.
If Mid(文件名全称, I, 1) = "." Then ' If the current position is '.' then...
ExtPos = I ' ...Change the ExtPos to the number.
For J = Len(文件名全称) To 1 Step -1 ' Do the Same...
If Mid(文件名全称, 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
Exit For ' Since we found it, don't search any more.
End If
Next
If PthPos > ExtPos Then
Exit Function ' No extension.
Else
If ExtPos = 0 Then Exit Function ' If there is not extension, then exit sub.
提取扩展名 = Mid(文件名全称, ExtPos + 1, Len(文件名全称) - ExtPos) 'Messagebox the Extension
End If
End Function
'使用:
'字符串变量 = 提取扩展名("c:\B_sys\Explorer.exe")
Function 获取字符串字节数(字符串 As String)
On Error Resume Next
Dim WINNT_CHINESE
WINNT_CHINESE = (Len("中国") = 2)
If WINNT_CHINESE Then
Dim l, T, C
Dim I
l = Len(字符串)
T = l
For I = 1 To l
C = Asc(Mid(字符串, I, 1))
If C < 0 Then C = C + 65536
If C > 255 Then
T = T + 1
End If
Next
获取字符串字节数 = T
Else
获取字符串字节数 = Len(字符串)
End If
If Err.Number <> 0 Then Err.Clear
End Function
Function 获取文件名称(文件名全称 As String)
Dim 扩展名 As String
扩展名 = 提取扩展名(文件名全称)
获取文件名称 = StrReverse(Mid(StrReverse(文件名全称), 获取字符串字节数(扩展名) + 2))
End Function