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

Re: Select Case Limitations by mayayana

mayayana
Sun Sep 02 09:25:48 PDT 2007

I've noticed that, too. As far as I know
it only accepts commas, not expressions,
despite the fact that the WSH docs call
the Case... values expressions. You just
have to use extra Case checks combined
with extra If/End If checks. In your example
it will get rather messy.

> 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



Re: Select Case Limitations by Alexander

Alexander
Sun Sep 02 09:58:45 PDT 2007

02.09.2007 17:56, 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

You're fully right, To and Is are not supporetd as keywords in a
select-case statement.
Next <varname> at the end of for-in or for-each is also not
supported in VBS as opposed to VB(A).

Try this instead:

For i = 1 To Len(strFileName)

a = Asc(Mid(strFileName, i, 1))
Select Case True

Case a < 40, _
a > 41 And a < 48, _
a > 57 And a < 65, _
a > 90 and a < 97, _
a > 122:

validateFileName = 0

Case Else

validateFileName = 1

End Select

Next


MfG
Alex

Re: Select Case Limitations by ekkehard

ekkehard
Sun Sep 02 11:06:21 PDT 2007

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.


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.
>
>

Re: Select Case Limitations by dch3

dch3
Sun Sep 02 15:06:00 PDT 2007

Oh and all that validateTemplateName does is check to see if there are any
disallowed characters in the string that's passed in. It then returns True or
False

"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.
>
>