Re: Select Case Limitations by dch3
dch3
Sun Sep 02 15:02:01 PDT 2007
1) I was developing the code in VBA to ensure that the function threw out the
correct errors. I simply did a copy & past hence the next i
2) I ended up coming up with this without having too many if...thens...
Function validateTemplateName(strFileName)
'Restricts the template name to alphanumeric characters, parenthesis,
hyphens and spaces
Dim i
validateTemplateName = -1
i = 1
While validateTemplateName And i <= Len(strFileName)
intASCII = Asc(Mid(strFileName, i, 1))
Select Case intASCII
Case 33, 34, 35, 36, 37, 38, 39, 42, 43, 44, 46, 47, 58, 59, 60, 61,
62, 63, 64
validateTemplateName = 0
Case Else
If intASCII < 32 Or intASCII > 122 Then validateTemplateName = 0
If intASCII > 57 And intASCII < 65 Then validateTemplateName = 0
If intASCII > 90 And intASCII < 97 Then validateTemplateName = 0
End Select
i = i + 1
Wend
End Function
Not too happy with the first case but it gets the job done.
3) The function validates user input that is later used to SAVE, COPY,
RENAME and DELETE files via the FileSystemObject. (RENAME is actually COPY &
DELETE). strFileName is not actually a file name as in readme.txt
"ekkehard.horner" wrote:
> dch3 schrieb:
> > Does VBScript allow expressions such as "1 to 5" and "IS > 89"? The following
> > code works fine in VBA, however VBScript doesn't appear to 'TO' and 'IS >'
> > parts fo the case statement.
> >
> > For i = 1 To Len(strFileName)
> > Select Case Asc(Mid(strFileName, i, 1))
> > Case 1 To 39, 42, 43, 44, 46, 47, 58 To 64, 91 To 96, Is > 122
> > validateFileName = 0
> > Case Else
> > End Select
> > Next i
> If you want to check possible file names, using a RegExp may be easier
>
> Dim aTests : aTests = Array( _
> "autoexec.bat", "aut0exec.bat", "charsanddigits" _
> , "c:\with\path\file.123" _
> , "bad-", "bad{", "bad," _
> )
> Dim oRE : Set oRE = New RegExp
> oRE.Pattern = "[^\w\d.]" ' file name + extension
> oRE.Pattern = "[^\w\d.:\\]" ' full file spec
> Dim sTest
> For Each sTest In aTests
> WScript.Echo sTest, CStr( oRE.Test( sTest ) )
> Next
>
> and less errorprone - using Select Case lured Alexander into writing
> code that sets validateFileName to a value corresponding to the last
> character in strFileName.
>
>