Project

General

Profile

Renaming files

Added by Nathan Sullivan about 12 years ago

I wrote a simple batch file for Windows that renames all of the jpegs in a directory:

for /f %%a IN ('dir /b *.JPG') do exiv2 -r mv %%a

for /f %%a IN ('dir /b *.JPG') do ren %%a abcd_%%a

When exiv2 attempts to rename a file whose name already exists, it asks if I want to Overwrite Rename or Skip. Is there a way to make it accept Rename by default without interaction from the user? I have a couple thousand jpegs, and while it is rare this case occurs, it does happen occasionally and I want to avoid having to watch it.

Question unrelated to exiv2: I'm not that great at batch scripts, is there a way to combine the above two statements into one? I want to avoid having to loop through the entire directory renaming them twice. It would be better if did both actions one after the other for each file.


Replies (9)

RE: Renaming files - Added by Robin Mills about 12 years ago

Nathan

I'm not sure about disabling user interaction - I'll leave Andreas to speak about that. It maybe a simple enhancement request (add a -q == quiet mode). However my one line (bash) script (on the Mac) to sync the exif date with the file system is:

exiv2 -T *.jpg *.JPG *.jpeg *.JPEG

I believe that only updates the modified date or something - however I've found it totally adequate. There is no user interaction. For historical reasons, I use different magic on Windows (only because I wrote my batch script years before I knew about exiv2).

DOS/cmd.exe Question:
I believe DOS/cmd supports a syntax ( ... ) for multiple commands in a block. Something like:

for /f %%f in (*.jpg) do (
exiv2 ... %%f ....
rename ...
)

This is much the same way as C (or Perl's) { .. } (or Pascal's BEGIN .. END, or bash's while ... do .. done/ if then ... fi

Robin

RE: Renaming files - Added by Nathan Sullivan about 12 years ago

Found it:

for /f %%a IN ('dir /b *.JPG') do exiv2 -F mv %%a

The -F option forces the rename. Your option works too, obviously. Thanks for recommending that, doing it that way seems better.

The other renaming problem I'm having might be able to be done with exiv2 also:

I set the filenames of all the jpegs in a directory to the date/time stamp, I also want to add a prefix to the beginning of each file name, something like:

abcd_20090705_150047.jpg

where abcd_ is the prefix I want to add. I messed around the the -r option, but couldn't get it to do both the date/time stamp and the prefix at the same time.

exiv2 -r abcd test.jpg

Will rename the file test.jpg to abcd.jpg.

Is there a way to do both the date/time stamp and prefix all in one command?

RE: Renaming files - Added by Robin Mills about 12 years ago

Nathan

I find DOS/cmd batch scripts are limited for this kind of thing and usually prefer to use Perl (or Python). ActiveState Perl (and Python) is very easy to download and install.

It might be that you have to rename the files in two calls to exiv2. I'll be honest - I never ask exiv2 to rename files.

I believe Brad's organize tool has a lot of machinery to rename files based on meta data. http://dev.exiv2.org/boards/3/topics/show/44

organize.exe will run over directories (including optional recursive descent of a tree). It also has a 'dryrun' option so you can gain confidence about its intentions. It could well be that will do everything you want - without any batch script magic! Having said that, I believe organize is primarily a tool for organizing images into a structured tree, you'll have to investigate its ability to rename individual images.

The default msvc/exiv2.sln build solution doesn't build organize because it would introduce a considerable dependancy on boost (which isn't required by exiv2 and the other sample applications). So exiv2+organize.sln (and organize.vcproj) is provided to build organize.exe on Windows and documented in exiv2/msvc/ReadMe.txt. If you want to play with organize.exe without spending the time installing all the boost stuff, I'll be happy to post a prebuilt binary for you to download and use. Of course, if organize meets your needs, I encourage you to install boost as you will be able to rebuild organize.exe with future releases of exiv2.

Let me know if you'd like me to post organize.exe for you.

Robin

RE: Renaming files - Added by Nathan Sullivan about 12 years ago

Brad's organize tool is pretty awesome, something I'll definitely be using, but it doesn't quite do what I want. His sorts the files into directories based on the meta data, but doesn't rename them. And I would use python, but I'm less familiar with that than with a batch script. I got a nice chunky book on Python 3 but haven't cracked it open yet.

I figured out a command to do what I want:

exiv2 mv -r abcd_%Y%m%d_%H%M%S test.jpg

So now my batch script looks like:

for /f %%a IN ('dir /b *.JPG') do exiv2 -F mv -r abcd_%%Y%%m%%d_%%H%%M%%S %%a

(There are two % for each variable so the batch script will recognize it as %Y instead of just Y, and execute correctly.)

Thanks for taking the time to help me though, I really appreciate it.

RE: Renaming files - Added by Robin Mills about 12 years ago

That's great. I'm glad this has a happy ending. I wonder if this will inspire Brad to add individual file renaming to organize? Oh, well - that's Brad's business. I'm really pleased that you've found what you want. I love the %% - so ugly - so necessary. I recently had a script that required \\\\ 4 slashes in loads of places. It's easy to hate software.

Incidentally, I noticed that Brad published a binary version for windows of organize.exe on the page referenced above. So you can download from there. And Andreas has said he'll probably build and publish binaries of organize.exe in future.

I would like to encourage you to learn Python because it's a very elegant language. Having said that, I don't use it as often as I should and resort to Perl. When in trouble/under pressure/laziness - I reach for the tool I know.

Robin

RE: Renaming files - Added by Andreas Huggel about 12 years ago

Nathan,

Just curious, is the for-loop really necessary? How is that different from

exiv2 -F -r 'abcd_%Y%m%d_%H%M%S' *.JPG

Of course I might be totally off-track; I'm not familiar with the Windows scripting syntax.

Andreas

RE: Renaming files - Added by Nathan Sullivan about 12 years ago

Robin,
I did download the binary version on the page you referenced. Very nice addition that.

Andreas,
Apparently no difference at all! Even better! Thanks.

I am getting a strange error when I do that though, if I execute:

exiv2 -F -r abcd_%Y%m%d_%H%M%S *.JPG

And the files being renamed are: test.jpg, test1.jpg, etc.. I get this error:
test.jpg: Failed to open the file
test1.jpg: Failed to open the file
...etc..

But, all the files are still renamed correctly: abcd_20090705_150047.jpg, and so on..
This error didn't occur with the for-loop.

RE: Renaming files - Added by Andreas Huggel about 12 years ago

That's really strange. An inconsistency with case sensitivity? What if you use

exiv2 -F -r abcd_%Y%m%d_%H%M%S *.jpg

with *.jpg instead of *.JPG in the command for your test files?

Andreas

RE: Renaming files - Added by Nathan Sullivan about 12 years ago

Whoops. That was completely my fault. I had:

exiv2 -F -r abcd_%Y%m%d_%H%M%S *.jpg *.JPG

When I ran it without the extra *.JPG it worked perfectly. I should've had *.jpeg in place of the second *.JPG.

Within my test files I have: .jpeg .JPEG .jpg .JPG, all get converted correctly with no error messages when I use:

exiv2 -F -r abcd_%Y%m%d_%H%M%S *.jpg *.jpeg

    (1-9/9)