photools.com Community

IMatch Discussion Boards => AI, FAQ, Workflow, Tutorials, Tips & Tricks => Renaming and Distributing Files => Topic started by: Mike on May 09, 2023, 11:26:58 PM

Title: Batch Rename/Replace File Extensions
Post by: Mike on May 09, 2023, 11:26:58 PM
Hi,

I normaly would expect the renamer to be the tool for my problem, but this doesn't seem to be implemented that way, so does anybody know a solution for the case we need to batch rename/replace file extensions?

I just discovered that more than 1000 image files (that have been in the database for a long time) are using an incorrect extension. They are actually WEBP files, but incorrectly use the JPG extension.

I tried an older app I found in the forum, that was meant for that, but it doesn't seem to work in the current iMatch.
Quote from: Jingo on December 21, 2017, 02:35:32 PMHi all... created a quick app that renames the extensions of files from within IMatch....

Any other ideas? Thanks in advance
Title: Re: Batch Rename/Replace File Extensions
Post by: Mario on May 10, 2023, 08:44:50 AM
Please do not post questions in a board that is intended to post tips & tricks to other users.
Don't just pick any board when posting. Read the description under each board name before posting.
I will move your post there this time.

The Renamer does not change file extensions - for obvious reasons.
This would open a venue for users to shot themselves into the foot big time.

Why don't you do a simple

ren *.webp *.jpg
on a command prompt in the folder containing the files with the wrong extensions?
This changes the webp extension into jpg while keeping the original file name.
Quick and easy, no special tools required.

Select the folder containing the files in IMatch and press <Ctrl>+<C>
Open a Command Prompt from the START menu.
Type cd and then press <Ctrl>+<V> to insert the folder name. Press <Enter>.
Note: If the folder in on another drive, type in the drive letter followed by : first and press <Enter>.

Title: Re: Batch Rename/Replace File Extensions
Post by: Mike on May 10, 2023, 10:17:07 AM
Thanks for the idea and yes, you are right, I shouldn't have posted the question on this board. Sorry!

Some aspects that make dealing with the extensions a bit difficult in my case:


I wanted to avoid having to install external tools, so your idea with the command suits me.

Possible steps:

Title: Re: Batch Rename/Replace File Extensions
Post by: bekesizl on May 10, 2023, 11:04:01 AM
Is there maybe a possibility in IMatch to make a list of filenames with full path of selected files to a textfile or clipboard?

This could be edited into a batch file to let Windows rename the selected files.
e.g.
Export:
c:\SomeFolder\SomeSubFolder\FileToRename.jpg
Edited command:
ren c:\SomeFolder\SomeSubFolder\FileToRename.jpg c:\SomeFolder\SomeSubFolder\FileToRename.webp
Title: Re: Batch Rename/Replace File Extensions
Post by: Mario on May 10, 2023, 11:21:47 AM
QuoteIs there maybe a possibility in IMatch to make a list of filenames with full path of selected files to a textfile or clipboard?
Select the files in a File Window and press <Ctrl>+<C>

See: File Management in IMatch (https://www.photools.com/help/imatch/file_management.htm) and here Keyboard Shortcuts (https://www.photools.com/help/imatch/file_management.htm?dl=h-2)

If you need more detailed control, you can e.g. use the Copy Data app or the Text Export (https://www.photools.com/help/imatch/impexp_text.htm) feature.
Using the Text Exporter with some clever variables could even produce your entire Batch File.
For example, use this variable:
ren "{File.FullName}" "{File.FullName|replace:.jpg==.webp}"
This produces lines like:

ren "c:\images\beach.jpg" "c:\images\beach.webp"

For each file you drop onto the text exporter.
Title: Re: Batch Rename/Replace File Extensions
Post by: Mike on May 10, 2023, 11:56:09 AM
Thank you both for the interesting approaches! 
I will try to implement that.
Title: Re: Batch Rename/Replace File Extensions
Post by: Mike on May 10, 2023, 04:07:30 PM
At first glance everything seems simple, and yet even a small test does not seem to work. I saved the Batch-File as UTF-8, and also tried the ANSI format.

The test batch-file (without the dotted lines, of course) contains the following text. Other versions have also failed.

---------------------------------------
@echo off

ren "E:\TEST FOLDER\SUBFOLDER A\FILE A.jpg" "E:\TEST FOLDER\SUBFOLDER A\FILE A.webp"
ren "E:\TEST FOLDER\SUBFOLDER B\FILE B.jpg" "E:\TEST FOLDER\SUBFOLDER B\FILE B.webp"
ren "E:\TEST FOLDER\SUBFOLDER C\FILE C.jpg" "E:\TEST FOLDER\SUBFOLDER C\FILE C.webp"

pause
---------------------------------------

The cmd.exe response is:

syntax error.
syntax error.
syntax error.

What could be wrong with the lines?


Any idea what I could be doing wrong?

PS. The chatGPT open Ai recommends the same content for the batch file.
Title: Re: Batch Rename/Replace File Extensions
Post by: thrinn on May 10, 2023, 04:23:17 PM
Well, the help for the ren command (ren /?) tells me:

Sie können kein neues Laufwerk und keinen anderen Pfad für die Zieldatei angeben.
[You can not enter a new drive or path for the target].

So, try to use the following expression in the Text Exporter:
ren "{File.FullName}" "{File.NameExt|replace:.jpg==.webp}"
This will result in something on the lines of:
ren "D:\Users\Thorsten\Pictures\P1050640-2.jpg" "P1050640-2.webp"
Title: Re: Batch Rename/Replace File Extensions
Post by: axel.hennig on May 10, 2023, 04:23:33 PM
I think it should be

--------------------------------------
@echo off

ren "E:\TEST FOLDER\SUBFOLDER A\FILE A.jpg" "FILE A.webp"
ren "E:\TEST FOLDER\SUBFOLDER B\FILE B.jpg" "FILE B.webp"
ren "E:\TEST FOLDER\SUBFOLDER C\FILE C.jpg" "FILE C.webp"

pause
---------------------------------------

Title: Re: Batch Rename/Replace File Extensions
Post by: Mario on May 10, 2023, 04:29:11 PM
My bad. The second argument of ren of course takes only a file name, not a path and file name.
The correct variable would thus be:

ren "{File.FullName}" "{File.NameExt|replace:.jpg==.webp}"


Note: When you rename files outside of IMatch that way, IMatch will lose track of the files and mark them as off-line.
You need to relocate them individually afterwards if you want to keep the associated data.

See Off-line Folders and Files (https://www.photools.com/help/imatch/offline-files.htm)
Title: Re: Batch Rename/Replace File Extensions
Post by: Mike on May 10, 2023, 04:40:18 PM
Thank you both! That was the solution. It works!

It was my first time trying to create a batch file in Windows, so I was a bit lost  ;)

Now I can try to apply this to the real files...
Title: Re: Batch Rename/Replace File Extensions
Post by: Mike on May 10, 2023, 04:45:24 PM
Quote from: Mario on May 10, 2023, 04:29:11 PMMy bad. 
Absolutely no problem!

Quote from: Mario on May 10, 2023, 04:29:11 PMNote: When you rename files outside of IMatch that way, IMatch will lose track of the files and mark them as off-line.
You need to relocate them individually afterwards if you want to keep the associated data.
I know that, only in this case, luckily, it won't be a problem.

Thanks!
Title: Re: Batch Rename/Replace File Extensions
Post by: thrinn on May 10, 2023, 04:47:43 PM
...but take extra care to read Mario's note about renaming files outside of IMatch! IMatch will not know that xyz.webp is the same file as xyz.jpg before. You mentioned about 1300 files scattered over multiple folders, and you will have to make a manual "Relocate" for each of these files.

I just found this rather old  thread (https://www.photools.com/community/index.php/topic,7572.msg52787.html#msg52787) with an App written by Jingo. Unfortunately, the attached ZIP file seems to be broken, but maybe Jingo can upload the App again - if it still works, that is.

[Edit: Just saw that you are aware of the Relocate - so forget what I wrote above]
Title: Re: Batch Rename/Replace File Extensions
Post by: Mike on May 10, 2023, 05:05:19 PM
I tested Jingo's app this morning but it doesn't seem to be working in the current iMatch. I was able to download and install it and I changed the extensions in it manually but nothing happened after clicking. The required files were of course selected.

Of course, it would be handy if the app worked and was available in iMatch. Who knows if or when we'll encounter any incorrect file extensions again.

At least I know the batch file solution now...
Title: Re: Batch Rename/Replace File Extensions
Post by: thrinn on May 10, 2023, 05:41:56 PM
I just tried the App again (after extracting it with 7Zip instead of the Windows built-in ZIP support) and it worked. The "Change from MOV to mp4" button only renames MOV files, but the "Change file extension" button respects the source and destination extensions I entered.
Title: Re: Batch Rename/Replace File Extensions
Post by: Mario on May 10, 2023, 06:08:04 PM
Quote from: thrinn on May 10, 2023, 04:47:43 PMI just found this rather old  thread (https://www.photools.com/community/index.php/topic,7572.msg52787.html#msg52787) with an App written by Jingo.
This is what I mean. A 5 year old thread with an app downloaded about 20 times in total...

Having to change a large number of file name extensions is such a rare thing, it does not really make a use case for a built-in feature in IMatch. I don't know under which circumstances .WEBP files could end up with a .JPEG file extension, but I guess it's really a rare situation.

The Text Exporter did a good job (after I've fixed my mistake).
Title: Re: Batch Rename/Replace File Extensions
Post by: Mike on May 11, 2023, 09:20:25 AM
Quote from: thrinn on May 10, 2023, 05:41:56 PMthe "Change file extension" button respects the source and destination extensions I entered.
I did exactly the same with no effect. Maybe I'll test that again sometime and see if I can discover the cause of the failure.


"I don't know under which circumstances .WEBP files could end up with a .JPEG file extension, but I guess it's really a rare situation."
I received some files that way, others come from downloads in the course of extensive research. Apparently similar things have happened in many places, whether accidental, systemic, or intentional for some purpose that escapes me.
Knowing what the occasional problem is, I'll be better able to prevent it in the future.


"The Text Exporter did a good job (after I've fixed my mistake)."
Yes, that's right
Title: Re: Batch Rename/Replace File Extensions
Post by: Jingo on May 19, 2023, 03:36:23 PM
Like Mario/Thrin mentioned, the app is many years old and written in kindness for a specific user purpose.  However, I just tested it and it still worked.. just change MOV to your extension and MP4 to whichever and click the CHANGE FILE EXTENSION button... if your selected files extension matches the new one, it will change it successfully!

Enjoy!