vbScript - Identify File by Perceived Type UserPageVisits:755 active 80 80 DaniWeb 561 60 2018-06-22T02:52:51+00:00 https://www.daniweb.com/programming/code/516435/vbscript-identify-file-by-perceived-type

vbScript - Identify File by Perceived Type

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

755 Views
About the Author

I completed my Computer Science degree at the University of Manitoba in 1976. I did two and a half years of programming in medical research followed by twenty-nine years at Manitoba Hydro (electric utility). Most of that was spent on doing development and maintenance on an AGC/SCADA (real-time programming/process control) system. The last ten years of that was spent doing application and infrastructure support and development. I have programmed in FORTRAN (mostly), APL, PL/1, COBOL, Lisp, SNOBOL, ALGOL, Assembler (several flavours), C, C++, Paradox, VB, vbScript and more recently, Python. I am married with two grown children of whom I am very proud, and a most beautiful wife. I am currently retired (and loving it).

code snippet
''#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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.