VBS TYPE MISMATCH: [STRING]

marcel ethan

Total Post:105

Points:735
Posted by  marcel ethan
 1229  View(s)
Ratings:
Rate this:
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.

  1. Vijay Shukla

    Post:100

    Points:700
    Re: vbs type mismatch: [string]

    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

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!