Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Characters in file name 3

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,509
US
Windows already prevents some characters in a file name:

FileName_jyw7bw.png


But now the other system I am sending files to wants me to prevent a user from using other characters in a file name like: [blue][tt]& ‘ # { } % ~ ;[/tt] [/blue]

Checking for one character is easy, just do [tt]InStr()[/tt], but is there an easy way to check for a list of those characters in a file name?

I can write a simple Function and check each character one-by-one, but there could be an easier, better, (lazy?) way?

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Maybe try regex with a pattern like this [pre][\\/:\*\?"<>\|&'#\{\}%~;][/pre]
 
Here, have a couple of options ...

Code:
[COLOR=blue]Public Function CleanFilename(strFilename As String) As String
    With CreateObject("vbscript.regexp")
        .Global = True
        .Pattern = "[<>:""/\\|?&‘#{}%~;]"
        CleanFilename = .Replace(strFilename, "")
    End With
End Function

Public Function CleanFilename2(strFilename As String) As String
    Dim lp As Long
    
    For lp = 1 To Len(strFilename)
        If InStr("<>:""/\|?&‘#{}%~;", Mid(strFilename, lp, 1)) = 0 Then CleanFilename2 = CleanFilename2 & Mid(strFilename, lp, 1)
    Next
    
End Function[/color]
 
Thank you.

I would rather NOT rename the files since they could contain Company names. I would rather rely on the user to do that. I just want to inform them about the 'bad' characters in a file name.

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
With pure VBA:
[pre]MsgBox sTestString Like "*[[&‘#{}%~;]]*"[/pre]

combo
 
combo, that's awesome! I never heard about LIKE operator before.
 
The pattern on the right side of 'Like' operator is flexible and sufficient for basic comparisons, details from MS here.

combo
 
It's a pity, that VBScript doesn't have this LIKE operator too
 
A pity maybe, but since Like is just a simplified regexp, if you really miss it you could replace it with a regexp-using function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top