VB6如何过滤如下 \ / : * ?"<>|字符

来源:百度知道 编辑:UC知道 时间:2024/07/05 14:27:58
我的程序要创建文件名,必须过滤以上字符,要不然会报错。
FileName的字符是由用户输入的,如何过滤掉FileName 中可能包含的这些字符。给个实现的代码谢谢!
dim FileName as string
FileName
比如:
FileNames = "\||New \Fi/?*/ : * ? < <:l>> e|"
过滤成 NewFile

Dim FileName As String, nStr As String, Str1 As String
FileName = "ddd*g.txt" '文件名举例
nStr = "\ / : * ? " & Chr(34) & " < > |" '要过滤的字符
For i = 1 To Len(nStr)
Str1 = Mid(nStr, i, 1)
If Str1 <> " " And InStr(FileName, Str1) > 0 Then
MsgBox "文件名中不能包括以下字符:" & vbCrLf & vbCrLf & nStr, vbInformation
Exit For
End If
Next

补充:-------------------------------------
'这样可以完全达到你的要求:
Private Sub Command1_Click()
Dim FileName As String, nStr As String, mStr As String
Dim S As Long, I As Long, Str1 As String

FileName = InputBox("请输入文件名:", "过滤", "\||New \Fi/?*/ : * ? < <:l>> e|") '文件名举例
If FileName = "" Then Exit Sub
nStr = " \/:*?" & Chr(34) & "<>|" '要过滤的字符
mStr = FileName
Do
For I =