Computing.Net > Forums > Programming > Identify and Rename Graphic Files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Identify and Rename Graphic Files

Reply to Message Icon

Name: transitionality2
Date: September 17, 2008 at 06:34:29 Pacific
OS: Windows XP Pro SP3
CPU/Ram: Athlon XP 1800 512 MB
Product: N/A
Comment:

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:01

identify 2.jpg
2.jpg PNG 800x1297 800x1297+0+0 8-bit PseudoClass 9c 9.61kb 0.080u 0:01

So 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.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: September 17, 2008 at 08:15:17 Pacific
Reply:

not tested

===================================
@echo off
setLocal EnableDelayedExpansion

for /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


0

Response Number 2
Name: transitionality2
Date: September 17, 2008 at 13:47:33 Pacific
Reply:

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.


0

Response Number 3
Name: transitionality2
Date: September 17, 2008 at 13:59:19 Pacific
Reply:

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!


0

Response Number 4
Name: Mechanix2Go
Date: September 18, 2008 at 03:35:00 Pacific
Reply:

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 EnableDelayedExpansion

for %%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


0

Response Number 5
Name: transitionality2
Date: September 18, 2008 at 04:39:41 Pacific
Reply:

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?


0

Related Posts

See More



Response Number 6
Name: transitionality2
Date: September 18, 2008 at 04:45:05 Pacific
Reply:

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.


0

Response Number 7
Name: Mechanix2Go
Date: September 18, 2008 at 08:04:36 Pacific
Reply:

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 EnableDelayedExpansion

for %%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


0

Response Number 8
Name: transitionality2
Date: September 19, 2008 at 00:33:27 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Dir-stack entries with li... weekly backups



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Identify and Rename Graphic Files

Copy and Rename Batch File www.computing.net/answers/programming/copy-and-rename-batch-file/15297.html

Copy and rename mmost recent file www.computing.net/answers/programming/copy-and-rename-mmost-recent-file/19636.html

Copy and rename same file 1000 times www.computing.net/answers/programming/copy-and-rename-same-file-1000-times/19839.html