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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invalid filename characters and REPLACE functions 1

Status
Not open for further replies.

AnonGod

IS-IT--Management
Jun 5, 2000
223
Hello!

I let users update a text field in a database to have any characters they want. Later I pull the name they entered and look for an image matching the name they entered.

Now there are reserved characters that you cannot use in a filename, so I want to strip them out of the name when I go and look for it. But these are allowed in my database. The reserved list is (so windows explorer says):
\ / : * ? " < > |

Is there a built in asp/vbs function I can use to strip these out or am I looking at 9 replace functions?

Thanks!
:) -Andrew

alien.gif

[Signature modified by admin request]
-TAG
anongod@hotmail.com
'Drawing on my fine command of language, I said nothing.'
 
Regular expressions are a powerful way to do it.

If you just want a cheap dirty hack you could do this:
Code:
Dim strBad, iCount
strBad = "\/:*?"<>|"
For iCount = 0 to Len(strNewFileName)
 if instr(strBad, mid(strNewFileName, iCount, 1)) then
  strNewFileName = Left(strNewFileName, iCount - 1) _
                 & Mid(strNewFileName, iCount + 1)
 end if
Next
 
Hmm, now that I look at it perhaps that loop that I just posted would miss the case of more than one invalid characters in a row.
 
Oh I don't mean to discourage learning Regular expessions.

I was just trying to show a way to use a loop with an instr.

Here is a way to do it without having to worry about 2 bad characters in a row:
Code:
Dim strBad, iCount, ThisCharacter strKeepers
strBad = "\/:*?"<>|"
For iCount = 0 to Len(strNewFileName)
 ThisCharacter = mid(strNewFileName, iCount, 1)
 if Not cBool(instr(strBad, ThisCharacter)) then
  strKeepers = strKeepers & ThisCharacter 
 end if
Next
Instead of cutting out the bad characters, it does the opposite and keeps only the good ones.
 
Wow, powerful stuff, thanks!

:) -Andrew

alien.gif

[Signature modified by admin request]
-TAG
anongod@hotmail.com
'Drawing on my fine command of language, I said nothing.'
 
Here's what I came up with - It appears to be working. Is there anything that I maybe didn't do or should be aware of? Maybe a quicker way to include all the characters I DO want to allow.

Thanks for the help.

:) -Andrew
Code:
Dim objRegExp, str
Set objRegExp = New RegExp

str = "This Is|A=Test+Name-OK?_Good...~`:)"

objRegExp.Pattern = "[^a-zA-Z_0-9\=\+\-\.\`\~\!\@\#\$\%\^\&\(\)]"
objRegExp.Global = True
msgbox(str)
str = objRegExp.Replace(str,"")
msgbox(str)

alien.gif

[Signature modified by admin request]
-TAG
anongod@hotmail.com
'Drawing on my fine command of language, I said nothing.'
 
Excellent, thank you.

:) -Andrew

alien.gif

[Signature modified by admin request]
-TAG
anongod@hotmail.com
'Drawing on my fine command of language, I said nothing.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top