VB6如何过滤如下 \ / : * ?"<>|字符
我的程序要创建文件名,必须过滤以上字符,要不然会报错。FileName的字符是由用户输入的,如何过滤掉FileName中可能包含的这些字符。给个实现的代码谢谢!dimFi...
我的程序要创建文件名,必须过滤以上字符,要不然会报错。
FileName的字符是由用户输入的,如何过滤掉FileName 中可能包含的这些字符。给个实现的代码谢谢!
dim FileName as string
FileName
比如:
FileNames = "\||New \Fi/?*/ : * ? < <:l>> e|"
过滤成 NewFile 展开
FileName的字符是由用户输入的,如何过滤掉FileName 中可能包含的这些字符。给个实现的代码谢谢!
dim FileName as string
FileName
比如:
FileNames = "\||New \Fi/?*/ : * ? < <:l>> e|"
过滤成 NewFile 展开
5个回答
展开全部
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
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
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家提供动态代理服务器服务的企业,旨在帮助用户更好地管理网络访问和安全。以下是一些关于StormProxies的IP动态代理服务的特点:1. 高匿名性:StormProxies的动态代理服务器具有高匿名性,可以有效...
点击进入详情页
本回答由Storm代理提供
展开全部
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
你可以试试
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ban = Array("\", "/", ":", "*", "?", """", "<", ">", "|") '要过滤的字符
For i = 0 To Ubound(ban)
If InStr(FileName, ban(i)) <> 0 Then
MsgBox "文件名包含禁止字符"
Exit For
End If
Next
For i = 0 To Ubound(ban)
If InStr(FileName, ban(i)) <> 0 Then
MsgBox "文件名包含禁止字符"
Exit For
End If
Next
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询