Does anyone have a snip of code that will parse a string for any of those characters that cannot be used in a file-save name and replace it? i.e. *.”/[]:;|=,
I’ve got an idea on how it can be done, but it’s getting too long and too slow.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Parsing for forbidden characters in file name
Here’s an alternative version:
Code:Public Function IsValidFile(sFileName As String) As Boolean IsValidFile = Not (sFileName Like "*[/|:*?""]*") End Function
Hans – thanks for this. I’d like to go one step further though – If a forbidden character exists, to replace it with a “-“. I’m taking the file save name from ‘subject’ text users enter into a text box, and I’m not going to be able to get them to change their behaviour.
You could use this function (more along the lines of Jan Karel’s suggestion):
Function CleanName(sFileName As String) As String Const sInvalidChars As String = "/|:*?""" Dim lCt As Long CleanName = sFileName For lCt = 1 To Len(sInvalidChars) CleanName = Replace(CleanName, Mid(sInvalidChars, lCt, 1), "-") Next lCt End Function
Example of usage:
Dim sName As String
sName = CleanName(InputBox(“Please enter a file name”))
Here is an example of Jefferson’s suggestion of using regular expressions:
Function CleanName(sFileName As String) As String With CreateObject("VBScript.RegExp") .Pattern = "[/\|:*?""]" .Global = True CleanName = .Replace(sFileName, "-") End With End Function
Warning: this way of using regular expressions is very slow – this version of the CleanName function is more than 30 times slower than the version I originally posted!
I suspect that regexp would come into its own if you had to do a lot of expression parsing within one procedure – you’d create the VBScript.RegExp object once and get a lot of mileage out of it. But here, the object gets used only once.
Bowlie wants to use the function to clean a filename entered by the user. Compared to the time needed to enter the filename, the execution time of either version is insignificant.
Like this maybe?
Public Function IsValidFile(sFileName As String) As Boolean '------------------------------------------------------------------------- ' Procedure : IsValidFile ' Company : JKP Application Development Services (c) ' Author : Jan Karel Pieterse ([url]www.jkp-ads.com[/url]) ' Created : 31-10-2009 ' Purpose : Returns TRUE if filename contains no invalid characters '------------------------------------------------------------------------- Const sInvalidChars As String = "?[]:|*!" Dim lCt As Long If (Len(sFileName) > 0) Then For lCt = 1 To Len(sInvalidChars) If InStr(sFileName, Mid(sInvalidChars, lCt, 1)) > 0 Then IsValidFile = False Exit Function End If Next IsValidFile = True End If End Function
If you have free time on your hands to learn about how to write VBScript regular expression patterns, you could do the replace without looping. See the WildReplace function in [post=’470690′]post #470690[/post].
== Edit ==
The syntax I referred to is documented here: VBScript > Pattern Property on MSDN.
Jefferson – thanks, I think but that is a little further along the learning curve for me …
I used Hans’ suggestion and frankly see no lag time at all. I’ve also tested with input of up to 255 characters and still I doubt that anyone would really notice a lag.
Hans – thanks again, It works very nicely and I learned how a couple of new (to me) VB statements work.
Oh, a PS … My Macro creates a document, inserts variable info into various bookmarks, saves the document and then ends. However the next time I open the document it warns me of Macros. Once the document has been saved I don’t need the macro any longer. How do I get rid of the macro (and the macro warning)?
How does it do that?
Hans, sorry – you are right, it doesn’t. The user creates a new document using my template which autoruns my macro. The Macro displays a dialogue box, the user clicks on choices and enters text which is then inserted into bookmarks in the new document. Using one of the text fields the document is then saved and the macro ends.
Bowlie
The users can place the template in their User Templates or Workgroup Templates folder. If they trust all installed templates and add-ins (in the Trusted Sources tab of Tools | Macro | Security…), they will not get the macro warning, regardless of their macro security setting.
Another option is to break the link between the template and the document. This should be done at the very end of the macro, for execution of a macro in the template will stop the moment the document is no longer attached to the template.
The instruction is:
ActiveDocument.AttachedTemplate = “Normal.dot”
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications