vbs type mismatch: [string]

Hi,

I have the following code giving me an error and I can't understand why.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDir = objFSO.GetFolder(strOrigDir)
Set colFiles = objDir.Files

For Each strFile in colFiles
Select Case Right(strFile,3)
    Case "rar"
        strTarFile = strFile
        intIsRar = 1
        Exit For
    Case "avi" or "mkv" or "mp4"
        strTarFile = strFile
        Exit For
    Case Else
        WScript.Quit
End Select
Next

I receive the message Type mismatch: '[string:"avi"]' on the Select Case line and I can't understand why. I have checked the value of strFile using VarType to make sure I'm not going crazy and trying to compare a string to something I shouldn't be (VarType returns 8). Any ideas why this is incorrect?

Thanks in advance for any recommendations or solutions.

Last updated:6/20/2013 5:20:41 AM

1 Answers

Vijay Shukla
Vijay Shukla

Hi,

The problem is in the syntax of the Select Case statement. If you want multiple cases, you need to use a comma-delimited list:

Select Case Right(strFile,3)
    Case 'rar'
        strTarFile = strFile
        intIsRar = 1
        Exit For
    Case 'avi', 'mkv', 'mp4'
        strTarFile = strFile
        Exit For
    Case Else
        WScript.Quit
End Select

The 'type mismatch' is probably happening because it is trying to evaluate 'avi' or 'mkv' or 'mp4' as a Boolean statement.

Answer