I’m trying to extract the Compression attribute from several thousand jpeg files.
How can I do this with either Excel VBA or PowerShell (or some other method)?
Some users haven’t used the correct technique to convert tiff or png files to jpegs. They just edited the file extension directly in Explorer instead of using an app like Photoshop to properly change the file format. This is causing trouble in a downstream process.
After checking a few files, the problematic ones have ‘Uncompressed’ in that field…and I want to isolate these so they can be corrected.
Note that This answer does not provide the solution I need. The Compression attribute is not in the list of 308 attributes output by that method.
5
Answers
You can get it with API
GetFileAttributes
.The simplest method in my opinion would be via WMIC, which is native to windows and usable via CMD and Powershell. Here’s an example of the command and its output:
This output can be tweaked, summarized or isolated as well, like so:
See more in the wmic help 🙂
EDIT:
Figured I’d add exact value return, since you’re only looking for compression state:
Returns:
You can parse this within your script however you like.
For your purposes it may be sufficient to distinguish mangled files containing some other sort of image format from JPEGs by looking for the SOI tag that should be at the start of a genuine classical JPEG file namely 0xffd8. Occurrence of "JFIF" as text a few bytes in is also a pretty good indication of a JPEG (but no guarantee). MS Bitmap files mostly begin with "BM" but there other possibilities. And PNG starts 0x89504E47 which includes the letters PNG. ISTR GIF files start with "GIF". Finally TIFF will begin with either "II" or "MM". Then set the file extension back to being consistent with the file contents and you should be OK to go. This should be simple enough open the file read the first word to check what you actually have in the file data (may need additional heuristics for other esoteric image formats).
You could also try re-educating your users with a baseball bat.
Here is a PowerShell answer. Remember that
WMI
is not available in PowerShell Core. Use CIM.Is there a separate filesystem compression and image compression?
From my earlier comment – if your aim is really to id the actual file type (vs. relying on the file extension)