Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi. My problem is simple. I have a directory full of .jpg files but some of these are actually png files and have been misnamed. So I downloaded and installed ImageMagick, which is a set of commandline tools that processes graphics. Among these tools is one called identify which, when given a graphic file as an argument, outputs some information about the file, including the format. For instance:
identify 1.jpg
1.jpg JPEG 800x1297 800x1297+0+0 8-bit DirectClass 125kb 0.100u 0:01identify 2.jpg
2.jpg PNG 800x1297 800x1297+0+0 8-bit PseudoClass 9c 9.61kb 0.080u 0:01So far so good.
Now I just need to write a batch file of the following pseudocode:
For all jpeg files in current directory
{
Get filename of current file.Run identify on filename, store output in text variable.
If text variable contains the string "PNG"
Rename current file to .png.
}I would appreciate help with syntax. Thanks.

not tested
===================================
@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir/b/a-d *.jpg') do (
identify %%a | find "PNG" > nul
if not errorlevel 1 ren %%a %%~Na.PNG
)
=====================================
If at first you don't succeed, you're about average.M2

It didn't work. The call to FIND called a unix command-line utility in my path and it gave an error
c:/comman~1/unixco~1/find.exe: can't get status of PNG
for every file in the directory. The batch file ended up renaming all the JPG's to PNG's which is clearly not desired behavior.
I don't know what went wrong where but I think the part that parses the identify output is at fault. I also have a grep utility in my path, if that helps.

I substituted "grep" instead of "find" and now it works great. Thanks!
Would you mind explaining why you put in 'dir/b/a-d *.jpg' instead of just *.jpg? I don't quite understand what the prefix is doing there.
Also, could you extend the batch a little bit for general purpose use? Let's say the directory contains .jpg, .png and .bmp files any of which could be identified as JPEG PNG or BMP by identify. If you give me a 3-case example I think I can extend it to as many formats as needed. Thanks again!

If you have non-standard utils ahead of what's built into windows, it should surprise no one if things go wonky.
The /a-d means 'no directories; just files'. Probably not necessary, but won't hurt.
The straightforward way to accomodate othe EXTs is to 'wrap' with another FOR.
NOTE the *2* )
=================================
@echo off
setLocal EnableDelayedExpansionfor %%e in (JPG BMP PNG) do (
for /f "tokens=* delims= " %%a in ('dir/b/a-d *.%%e') do (
identify %%a | find "PNG" > nul
if not errorlevel 1 ren %%a %%~Na.PNG
)
)
=====================================
If at first you don't succeed, you're about average.M2

Thank you for bearing with me. I don't see a 2. It also seems like the tail end of the for loop is missing. Did you perhaps paste only part of the code?

Maybe I didn't specify the more general case clearly enough. Let us assume that we have files named *.jpg, *.png and *.bmp, and let us assume that any one of these files could be JPEG PNG or BMP regardless of name. So we would need to do three checks per file using identify.

First, I meant 'THERE ARE TWO )'
Call it end parenthesis or whatever. It's 29hex.
To check each file for each posible file type:
============================================
@echo off
setLocal EnableDelayedExpansionfor %%e in (JPG BMP PNG) do (
for /f "tokens=* delims= " %%a in ('dir/b/a-d *.%%e') do (
identify %%a | find "PNG" > nul
if not errorlevel 1 ren %%a %%~Na.PNG
identify %%a | find "BMP" > nul
if not errorlevel 1 ren %%a %%~Na.BMP
identify %%a | find "JPG" > nul
if not errorlevel 1 ren %%a %%~Na.JPG
)
)
=====================================
If at first you don't succeed, you're about average.M2

Oh you meant the two closing parentheses. Yeah I noticed that. And the more general case makes sense, now I can extend it to more formats if necessary. Thank you for your help.

![]() |
Dir-stack entries with li...
|
weekly backups
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |