Page 1 of 1

Renaming Media Files

Posted: 08 Jan 2017 03:22
by Orijohnal
How can I rename files already linked to records?

I like the way AS names media files as it copies them into FH but I need to go back and tidy up my existing media filenames. I've searched but can't find the procedure for doing this within FH.

Re: Renaming Media Files

Posted: 08 Jan 2017 07:23
by trevorrix
If you wish to rename a media file, in FH find the media item in Media > View All Media, then double click that media item thumbnail. At the bottom of the Media Record tab you will see the existing filename together with the Location of that file. Click the Location link which will automatically select the file for you, which you can right click and Rename. After you have renamed the file, change the last element of Linked File: in the Multimedia Object property box (which should be in view to the right of FH) to match the new filename.

Re: Renaming Media Files

Posted: 08 Jan 2017 10:17
by Jane
I wrote myself a plugin which renames the Files based on the titles of the Media records, it's not at plugin store quality, but you might be able to use it as a base. It prefixes the names with the Media Record Number, but it would be easy to change.

Re: Renaming Media Files

Posted: 08 Jan 2017 12:12
by johnmorrisoniom
Jane, knowing little about plugins, but using them a lot, How would I modify your plugin to just rename files to be the same as the internal title (No record ID prefix)
My naming conventions have changed over the years, and as such I have several 1000 records to catch up on.

Re: Renaming Media Files

Posted: 08 Jan 2017 13:47
by Orijohnal
Thanks Trevor, your method worked fine. I have a few hundred poorly named media files so I'll try Jane's plugin.

Re: Renaming Media Files

Posted: 08 Jan 2017 14:04
by Orijohnal
Thanks, Jane. I've checked out your plugin and the concept would work brilliantly. I'm tempted to indulge in the experiment of modifying the plugin, which I'm sure in the right hands would be elementary. In my case, however - a digression I must resist. If you find the time, johnmorrisoniom's request to replace the existing filename with the media title would be ideal!

A less efficient, albeit more intuitive solution for a future version of FH would be a "Rename media file" option in the media window context menu.



Thanks for the prompt reply.

J.

Re: Renaming Media Files

Posted: 08 Jan 2017 15:00
by tatewise
OK guys, here is how to modify the Plugin.

In Tools > Plugins make sure More>> is clicked so it says <<Less and the button bar on right is revealed.
Select the Plugin name and click Edit button to open the Plugin Editor.

About line 25 you will find the script line that composes the new file name from the Title:-
newfile = 'O'..id..'_'..string.gsub(fhGetItemText(ptr,'~.TITL'),'[_%W]','_')..'.'..ext
Replace it with:-
newfile = string.gsub(fhGetItemText(ptr,'~.TITL'),'[_%W]','_')..'.'..ext
i.e. delete the 'O'..id..'_'.. part which is prefixing the letter 'O' and RecordId from two lines above.

The remainder of that script line replaces every underscore & non-alphanumeric character '[_%W]' with an underscore '_' and suffixes the file extension. That is done to avoid illegal filename characters.

Use File > Save and the job is done.

Beware that if by any chance you have multiple Media records with the same Title then there will be a filename clash, and similarly if any other file in the same Media folder has the same filename. That is why the Plugin added the RecordId to ensure the filename is unique.

Re: Renaming Media Files

Posted: 08 Jan 2017 18:23
by Orijohnal
Brilliant. Works a charm.

Thanks Mike.

Re: Renaming Media Files

Posted: 21 Jan 2017 15:32
by johnmorrisoniom
Just got around to trying the modified plugin (No ID) and have noticed that the rename also replaces space and () with the _ character (Which windows does allow in filenames). Is there a way to stop this happening?
TIA John

Re: Renaming Media Files

Posted: 21 Jan 2017 17:36
by tatewise
Yes, the bit to change is the '[_%W]' in the gsub(...) function.
That identifies the characters to substitute as documented in plugins:understanding_lua_patterns|> Understanding Lua Patterns.

The '[set]' identifies the set of characters to be changed.
_ underscore represents itself (and is not strictly needed at all).
%W is the upper-case version of %w or all alphanumeric characters, so is all non-alphanumeric characters.

It is probably easiest to use '[^set]' that identifies the complement of the set of characters to be changed.
In other words, it lists the characters to retain unchanged, and all the others must be changed.
%w represents all alphanumeric characters to be retained
%( represents open parenthesis (the leading % is needed to inhibit its 'magic' meaning).
%) represents close parenthesis similarly.
space represents itself.

So the final definition is '[^%w%(%) ]' to replace '[_%W]'

Easy when you have been doing it as long as I have :lol:

Re: Renaming Media Files

Posted: 21 Jan 2017 20:43
by johnmorrisoniom
Brilliant,
Thanks Mike, i actually modified it slightly to include - to be left so used [^%w%(%)%- ]

Now a step further.

My media titles all have the media type keyword as the first word
i.e.

Marriage 1925 Douglas (St Georges) - (Page 240)
Baptism 1877 Douglas (St Matthews) - (Page 188)
Census 1901 IOM Douglas - RG13 piece 5305 folio 013 page 18

is it possible for the rename process to remove the 1st word from the string, so that the media titles stay the same but the filenames would become

1925 Douglas (St Georges) - (Page 240)
1877 Douglas (St Matthews) - (Page 188)
1901 IOM Douglas - RG13 piece 5305 folio 013 page 18

Regards
John

Re: Renaming Media Files

Posted: 21 Jan 2017 21:58
by tatewise
Yes, easy peasy!

Insert this extra line below the one we have been editing:
newfile = string.gsub(newfile,'^%a+ (.*)','%1')

The 1st quoted string says:
^ anchor to start of filename
%a any letter
+ repeated one or more times i.e. detect the first alphabetic word ending in a space
until a space character
( start capture ~ this is the magic use of ( and )
.* all other text through to end of filename
) end capture

The 2nd quoted string says:
%1 substitute everything with the 1st captured text

What is more difficult is removing those leading keywords from the actual filenames within the Media folder?

Re: Renaming Media Files

Posted: 22 Jan 2017 10:48
by johnmorrisoniom
Thanks Mike,
That works exactly as I want it to, I just have to watch out for filenames that start with the year (Ie already tweaked), But I have re-arranged the media tab columns to see that.
Thanks for your help
John

Re: Renaming Media Files

Posted: 22 Jan 2017 14:21
by tatewise
That extra Plugin line should have no impact of filenames that start with year, because it is only detecting leading alpha and will ignore leading numbers.

Re: Renaming Media Files

Posted: 22 Jan 2017 15:04
by PeterR
What is more difficult is removing those leading keywords from the actual filenames within the Media folder?
FileMenu Tools includes an Advanced Renamer… that can use Regular Expressions.

Re: Renaming Media Files

Posted: 21 Apr 2022 16:04
by MFriend
Jane wrote: ↑
08 Jan 2017 10:17
I wrote myself a plugin which renames the Files based on the titles of the Media records, it's not at plugin store quality, but you might be able to use it as a base. It prefixes the names with the Media Record Number, but it would be easy to change.
Hi Jane:
I recently gave titles to several hundred media files that just had numbers for the file name and title.

I'd love to be able to change most of them using your plugin. I'm getting an error when I try to run it (tried with just a few files to test). Maybe you have an idea on what is wrong? Here is the error:
Image

Thanks,
Matthew

Re: Renaming Media Files

Posted: 21 Apr 2022 16:28
by PeterR
Looks like a filename has no extension?

Re: Renaming Media Files

Posted: 21 Apr 2022 16:32
by Jane
It will need some changes to work on FH7. As the tag for Files is different on FH7.

Re: Renaming Media Files

Posted: 21 Apr 2022 17:06
by tatewise
See the posting Zooming Media images (20335) that has two updated versions of the plugin.

Also, the thread re naming source/media files (17412) has another updated variant.

It depends on exactly what you want the renaming to do.

Re: Renaming Media Files

Posted: 21 Apr 2022 17:54
by MFriend
Perfect! Thank you Mike, Jane and Peter.

I used the one with no id from 2022 (https://www.fhug.org.uk/forum/viewtopic ... 35#p122146) and it worked great. That saved me hours of tedious renaming one file at a time. FH 7 and you all are great!!

Matthew