VB6如何过滤如下 尀 ⼀ : * ?"<>|字符

2025年02月04日 03:43
有5个网友回答
网友(1):

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 = 1 To Len(nStr)
Str1 = Mid(nStr, I, 1)
S = InStr(mStr, Str1)
If S > 0 Then
mStr = Left(mStr, S - 1) & Mid(mStr, S + 1)
Exit For
End If
Next
If S = 0 Then Exit Do
Loop
MsgBox FileName & vbCrLf & vbCrLf & "过滤后的文件名是:" & vbCrLf & mStr, vbInformation
End Sub

网友(2):

1.用CommonDialog吧,那个遇到这些文件名直接过滤
你可以试试
Dim FileNames As String
CommonDialog1.ShowSave
FileNames = CommonDialog1.FileName

2.Dim FileNames As String
For i = 1 To Len(FileNames)
FileNames = "\||New \Fi/?*/ : * ? < <:l>> e|"
If Mid(FileNames, i, 1) <> "\" or "/" or ":" or "*" or "?" or Chr(34) 0r "<" or ">" or "|" then
NewFile=NewFile & Mid(FileNames, i, 1)
End If

网友(3):

ban = Array("\", "/", ":", "*", "?", """", "<", ">", "|") '要过滤的字符
For i = 0 To Ubound(ban)
If InStr(FileName, ban(i)) <> 0 Then
MsgBox "文件名包含禁止字符"
Exit For
End If
Next

网友(4):

Private Sub Text1_KeyPress(KeyAscii As Integer)
st = "\ / : * ?""<>|"
If InStr(st, Chr(KeyAscii)) > 0 Then KeyAscii = 0

End Sub

这样:
Private Sub Command1_Click()
st = "\ / : * ?""<>|"

filenames = "\||New \Fi/?*/ : * ? < <:l>> e|"
For i = 1 To Len(filenames)
st1 = Mid(filenames, i, 1)
If InStr(st, st1) = 0 Then File = File & st1
Next
filenames = File
End Sub

函数形式:

Function FileNames(File As String) As String
Dim st As String, St1 As String, File1 As String

st = "\ / : * ?""<>|"
For i = 1 To Len(File)
St1 = Mid(File, i, 1)
If InStr(st, St1) = 0 Then File1 = File1 & St1
Next
FileNames = File1

End Function

网友(5):

Dim FileName As String
Dim i As Long
For i = 1 To Len(FileName)
s=mid(filename,i,1)
If s = "\" Or s = "/" Or s = ":" Or s = "*" Or s = "?" Or s = "<" Or s = ">" Or s = "|" Then
FileName = Left(FileName, i - 1) & Right(FileName, Len(FileName) - i)
End If
Next