Bulk rename
-
Hello, I’m trying to use a chrome plugin to rename many photos in an album at once (using find and replace with regex in text fields), it correctly renames all the fields but it doesn’t trigger the auto save of WPPA and I can’t find any way to manually save the changed names when I edit them this way. A way to manually save all changes, or a way to bulk rename with regex would be very nice.
-
Your script has to focus a field, change the content and after that the focus has to leave the field. Then the onchange event occurs and does the magic.
I’m relying on an extension to do it for me and I can’t customize it to do something like that, I haven’t written my own script and don’t think I’m capable of doing it at the moment either unfortunately.
I have no idea what your script does, but keep the following in mind:
– The original files are stored in …/wp-content/uploads/wppa-source/album-<albumid>. These files are only saved when you have Keep source files active on Advanced settings -> Files. If you change the names of these files, you must also change the data in db field ‘filename’ in table
$wpdb->wppa_photos
identically, otherwise your sourcefiles are no longer findable. This is strongly discouraged.– The display and thumbnail files are named <id>.<extension>. Do never change this.
– If you want to change the names as they appear e.g. under the slides and thumbnails, you must use
wppa_update_photo( ['id' => $id, 'name' => $new_name] );
to get the nameslug updated as well and to trigger re-indexing for the search engine.You could write a custom photo procedure to run in maintenance -> II -> Item 36
This could look like (not complete as i do not know your preg_replace details):<?php $name = wppa_get_photo_item( $id, 'name' ); $new_name = <your preg_replace function on $name>; wppa_update_photo( ['id' => $id, 'name' => $new_name] );
Do NOT end the code with
?>
!!!Thank you, I will look into this. The thing I tried initially was just a simple chrome extension that searches and replaces text in input fields on a page, and I used it on the album admin edit page, it just doesn’t work because it edits them all at once and can’t trigger the auto save.
My goal is to clean up the photo names (as they appear under thumbnails), for example to add spaces before capital letters and things like that. I have thousands of photos so I’m hoping I won’t have to do it all manually.
I see. You can use the cusom photo proc as described above. If you tell me details of what to change into what, i will work that proc out and test it for you
They’re in a format like this “1955-001ExampleName” and I want it like “1955-001 Example Name”, but sometimes there are acronyms and hyphens like “1955-001ExampleName-ABC” and I want “1955-001 Example Name-ABC”
I don’t know if all of that is possible but any part of it would be helpful.
If i formulated as follows:
If a capital character is not preceeded by a ( -, a capital or a space ), insert a space before it
I am close?
Yes that sounds like exactly what I need
Do the following:
1. Copy the code below and paste it in Advanced settings -> II -> Item 36: Custom photo proc:
<?php $caps = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; $void = [' ','-','.','(','_']; $old_name = wppa_get_photo_item( $id, 'name' ); $new_name = ''; $i = 0; $prev = ' '; while ( $i < strlen( $old_name ) ) { $cur = substr( $old_name, $i, 1 ); if ( in_array( $cur, $caps ) ) { /* current is capital */ if ( ! in_array( $prev, $void ) && ! in_array( $prev, $caps ) ) { /* NOT preceeeded by cap or any void char */ $new_name .= ' '; /* insert space */ } } $new_name .= $cur; $prev = $cur; $i++; } if ( $old_name != $new_name ) { wppa_update_photo( ['id' => $id, 'name' => $new_name] ); wppa_log( 'obs', 'Photo '.$id.' name: '.$old_name.' changed into: '.$new_name); }
2. Wait for the green checkmark.
3. Click the Start! button.
4. Open Photo albums -> Logfile in a new tab (or click in a new tab Advanced settings -> Misc -> II -> Item 8: List logfile: the Show! button)
Wait for something like this on the logfile display:
Obs: on:11.06.2022 10:20:23: opajaapzelf (47567): Maintenance proc wppa_custom_photo_proc completed Obs: on:11.06.2022 10:20:18: opajaapzelf (47567): Photo 1088 name: 1955-001ExampleName changed into: 1955-001 Example Name Obs: on:11.06.2022 10:20:17: opajaapzelf (47567): Photo 619 name: 1955-001ExampleName-ABC changed into: 1955-001 Example Name-ABC Obs: on:11.06.2022 10:20:12: opajaapzelf (47567): Maintenance proc wppa_custom_photo_proc started. Allowed runtime: 55s.
and wait for the Maintenance proc wppa_custom_photo_proc completed message. (Note: the newest message is on top)
You may need to change anything anywhere to trigger background processes for re-indexing. I am not sure if this is required.
Perfect, thank you so much. I did notice one more thing now after doing it. Some of the photos have numbers in the names and I want spaces before those too.
Example: “1955-001 Example Name55-56 ABC” should be “1955-001 Example Name 55-56 ABC”
I don’t know if that’s easy to fix without conflicting with other conditions or not. If not I can fix the rest manually because the majority of the problems are now gone.
Perfect!
Should not be difficult to change it for the numbers, i will have a look tomorrow.
BTW: you can run the samem proc again if needed without problems for the existing fixesHere is the new code:
<?php $caps = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; $num = ['1','2','3','4','5','6','7','8','9','0']; $void = [' ','-','.','(','_']; $old_name = wppa_get_photo_item( $id, 'name' ); $new_name = ''; $i = 0; $prev = ' '; while ( $i < strlen( $old_name ) ) { $cur = substr( $old_name, $i, 1 ); if ( in_array( $cur, $caps ) ) { /* current is capital */ if ( ! in_array( $prev, $void ) && ! in_array( $prev, $caps ) ) { /* NOT preceeeded by cap or any void char */ $new_name .= ' '; /* insert space */ } } if ( in_array( $cur, $num ) ) { /* current is number */ if ( ! in_array( $prev, $void ) && ! in_array( $prev, $num ) ) { /* NOT preceeeded by number or any void char */ $new_name .= ' '; /* insert space */ } } $new_name .= $cur; $prev = $cur; $i++; } if ( $old_name != $new_name ) { wppa_update_photo( ['id' => $id, 'name' => $new_name] ); wppa_log( 'obs', 'Photo '.$id.' name: '.$old_name.' changed into: '.$new_name); }
Make sure you first completely remove the old version from the text area field before copying this version. Keep it for latere re-run after new items have been added with the same issues.
Great, now it’s truly perfect! Thanks again.
- The topic ‘Bulk rename’ is closed to new replies.