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.