vbScript - Identify File by Perceived Type

Updated Reverend Jim 1 Tallied Votes 914 Views Share

vbScript - Identify File by Perceived Type

Please see my post vbScript - The Basics for more details on vbScript.

There are times when you want to operate on all files of a given type. For example, you may want to enumerate all files in a folder or a drive that are recognized by Windows as video or audio. But how would you do that? There are so many video file types that it would be difficult to test for even the more common file extensions. Better to ask Windows to do the identification. The Windows Registry contains an key for all of the file extensions that it recognizes. A number of these keys have an associated entry with the name PerceivedType. The value of that entry is of type REG_SZ (zero terminated string) and is the recognized generic file type. It will be a value like "audio", "video", "text", etc. vbScript provides a way of creating, deleting, reading, and writing registry values.

For example, to get the PerceivedType for the extension "avi" you can do

Set wso = CreateObject("Wscript.Shell")
PerceivedType = wso.RegRead("HKCR\.avi\PerceivedType")

If there is no PerceivedType entry then this will throw an error so the easiest way around that is either

PerceivedType = ""
on error resume next
PerceivedType = wso.RegRead("HKCR\.avi\PerceivedType")
err.Clear

or

on error resume next
PerceivedType = wso.RegRead("HKCR\.avi\PerceivedType")

if err.Number <> 0 then
    PerceivedType = ""
    err.Clear
end if

I'm lazy so I prefer the first form.

Please note that this method does not verify that the file contents are valid for the perceived type.

''#region Header                                                                        '
''                                                                                      '
''  Name:                                                                               '
''                                                                                      '
''      PerceivedType.vbs                                                               '
''                                                                                      '
''  Description:                                                                        '
''                                                                                      '
''      If a file extension is perceived by Windows to be of a specific type then this  '
''      function will return a lower case string with that type name. For example, if   '
''      a file has extension "avi" or "mp4" and Windows recognizes those extensions,    '
''      this function will return the value "video".                                    '
''                                                                                      '
''      The perceived type is a string value fetched from the Windows Registry. If the  '
''      file extension does not have a PerceivedType value then "" is returned.         '
''                                                                                      '
''  Auxiliary Functions:                                                                '
''                                                                                      '
''      IsVideo(file)   -   returns True if PerceivedType = "video"                     '
''      IsAudio(file)   -   returns True if PerceivedType = "audio"                     '
''      IsText (file)   -   returns True if PerceivedType = "text"                      '
''                                                                                      '
''  Notes:                                                                              '
''                                                                                      '
''      file can be a file name or a Scripting file object.                             '
''                                                                                      '
''  Audit:                                                                              '
''                                                                                      '
''      2018-02-15  rj  added header                                                    '
''                                                                                      '
''#endregion                                                                            '

Function PerceivedType (file)

    Dim wso: Set wso = CreateObject("Wscript.Shell")
    Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")

    PerceivedType = ""
    on error resume next
    PerceivedType = Lcase(wso.RegRead("HKCR\." & fso.GetExtensionName(file) & "\PerceivedType"))
    err.Clear

End Function

Function IsVideo (file): IsVideo = PerceivedType(file) = "video": End Function
Function IsAudio (file): IsAudio = PerceivedType(file) = "audio": End Function
Function IsText  (file): IsVideo = PerceivedType(file) = "text" : End Function

''Test code                                                                             '
If StrComp(Wscript.ScriptName,"PerceivedType.vbs") = 0 Then
    WScript.Echo "file.avi", PerceivedType("file.avi")
    WScript.Echo "file.mp3", PerceivedType("file.mp3")
    WScript.Echo "file.txt", PerceivedType("file.txt")
    WScript.Echo "file.123", PerceivedType("file.123")
    WScript.Echo "file.rec", CStr(IsVideo("file.rec"))
End If