Author Topic: another plug for search and replace in file names  (Read 6505 times)

Offline chardinej

  • Member
  • **
  • Posts: 75
    • View Profile
another plug for search and replace in file names
« on: April 11, 2010, 08:07:41 AM »
Although substring extraction is a powerful tool, it has a lot of limitations to replace text in a filename when the filenames are not all the same format. This is where a search and replace text in file name feature in Rename file would be very useful. I am migrating to PM from Expression Media and this feature is one of the few XM has and PM doesn't.
John

MacBook Air i5/10.8 (Mountain Lion); PM 5 latest build

Offline Hayo Baan

  • Uber Member
  • ******
  • Posts: 2552
  • Professional Photographer & Software Developer
    • View Profile
    • Hayo Baan - Photography
Re: another plug for search and replace in file names
« Reply #1 on: April 11, 2010, 11:36:25 AM »
Can you give us an example of what you are trying to achieve here? Perhaps we can find a work-around for it, or perhaps this could lead to a great new feature  :)

Regards,
    Hayo
Hayo Baan - Photography
Web: www.hayobaan.nl

Offline chardinej

  • Member
  • **
  • Posts: 75
    • View Profile
Re: another plug for search and replace in file names
« Reply #2 on: April 11, 2010, 02:01:45 PM »
The key to needing this feature is that your filenames are not all in the same format. In other words, what occurs before the suffix filetype can be any number of characters long and in any format. You may ask at the outset- "well how did you arrive at this state?", and my answer is "it doesn't matter, let's assume we are just there, rather than discuss how to avoid it to begin with".

So you have the following file names:

20091004_AMGO_IMG_9866.CR2
20070510_PISI_4563.CR2
20090317_Oak_tree_IMG_8743.CR2
20081112_IMG_swamp_5792.CR2

And you want to remove "_IMG" from the filenames that have it. Clearly you can't just hash in to a particular location in the filename string and pull this substring out. What I need to do is have a search and replace which says: find every occurrence of "_IMG" and replace it with "" or whatever.

I would be trilled if there's a way to do this in PM already!

Thanks,
John


John

MacBook Air i5/10.8 (Mountain Lion); PM 5 latest build

Offline Hayo Baan

  • Uber Member
  • ******
  • Posts: 2552
  • Professional Photographer & Software Developer
    • View Profile
    • Hayo Baan - Photography
Re: another plug for search and replace in file names
« Reply #3 on: April 12, 2010, 04:13:05 AM »
Perhaps there is a work-around for what you want to achieve in PM as long as the things in the middle like AMGO, PISI,swamp, etc. are present somewhere in the metadata of the file.

  • From the filenames you gave as an example, I reckon the first part represents the date at which the image was taken and is present in all your files? If so, for this part you could either use {filenamebase:0,8} or {datesort}.
  • Then add an underscore _
  • Then add the metadate field representing the middle bit e.g., {object}
  • Then add another underscore _
  • And finally the last four digits like so {filenamebase:-4}

So the rename string would look something like this: {filenamebase:0,8}_{object}_{filenamebase:-4}

However, if the middle part is not part of some of the metadata fields, I think your best approach would be to rename the strings on the command-line. If you're on the Mac, the command could be something like this:
rensed s/_IMG// <files>
Where <files> is one or more filenames. rensed is a little script I wrote which renames a file using a rule. The script looks like this:
#! /bin/sh

ANSWER=''
RULE=$1
shift

for a in "$@"; do
    if [[ ! -e "$a" ]]; then
   echo "$0: File not found $a" 1>&2
    else
        NEWNAME=`echo $a | sed "$RULE"`
        if [[ "$NEWNAME" != "" && "$NEWNAME" != "$a" ]]; then
            if [[ "$ANSWER" != "A" ]]; then
                printf "$a ⇒\n$NEWNAME?"
                read -p " (y/n/a/q) " -n 1 ANSWER
                echo
            fi
            ANSWER=`echo $ANSWER | tr "[:lower:]" "[:upper:]"`
            if [[ "$ANSWER" = "Q" ]]; then
                exit 1
            fi
            if [[ "$ANSWER" = "A" || "$ANSWER" = "Y" ]]; then
                mv "$a" "$NEWNAME"
            fi
        fi
    fi
done

The script will ask your permission for each rename. Answer Y to rename, N to not rename this file, A to rename this file and all following files without asking, Q to quit completely.

Hope this helps and if you have any further questions, just ask  :)
    Hayo
Hayo Baan - Photography
Web: www.hayobaan.nl

Offline chardinej

  • Member
  • **
  • Posts: 75
    • View Profile
Re: another plug for search and replace in file names
« Reply #4 on: April 12, 2010, 04:57:49 AM »
Thank you very much Hayo. I must admit that although I've used Macs since 1984, and Max OS X since it came out, I have never dealved into scripting in Unix.

One key to your first method is that components of the filename should be found in some variable or another. For me unfortunately this is not the case, but maybe it is a lesson for the future!

If search and replace in filenames is just not possible for some reason in PM, then a useful addition to PM would be Functions that could act on variables. In a sense we already have the Substring function in the substring extraction but what about adding more functions like:

1. Length{variable} which would return the length of the string
2. Position{variable,"string"] which would return the position or number of characters from the left of the starting position of "String" in Variable. Then you could use the value of Position to hash into a string.
3. etc

John
« Last Edit: April 12, 2010, 04:59:26 AM by chardinej »
John

MacBook Air i5/10.8 (Mountain Lion); PM 5 latest build

Offline vAfotoriporter

  • Uber Member
  • ******
  • Posts: 1046
    • View Profile
    • Attila Volgyi photojournalist
Re: another plug for search and replace in file names
« Reply #5 on: April 12, 2010, 06:38:15 AM »
My first idea of a use for this requested feature would be if someone for any reason puts a name into the filename then discovers it was misspelled and wants to replace it with the correct version.

I second this request.
Working on Mac, OSX, iOS and with some Canons.
Allways shooting RAW.

http://www.volgyiattila.hu

Offline Hayo Baan

  • Uber Member
  • ******
  • Posts: 2552
  • Professional Photographer & Software Developer
    • View Profile
    • Hayo Baan - Photography
Re: another plug for search and replace in file names
« Reply #6 on: April 15, 2010, 01:54:46 PM »
Thank you very much Hayo. I must admit that although I've used Macs since 1984, and Max OS X since it came out, I have never dealved into scripting in Unix.

As long as you dare type a few commands in the Terminal, you should be able to use the method I described. If necessary I can detail this step by step for you, just let me know :-)

One key to your first method is that components of the filename should be found in some variable or another. For me unfortunately this is not the case, but maybe it is a lesson for the future!

In my workflow and file naming I simply name the file using the sortable versions of the date and time sorted (up to the miliseconds if encoded in the file. An abbreviation of the camera model used is added to cover those cases where there are two shots by different cameras shot at exactly the same time (I've had this happen). This filename has the advantage that when sorting on filename, the images appear in order, and each filename is unique and totally reproducible from the content of the file.
Admittedly, the filename this way is quite long and also non descriptive. Here comes the power of PM though, it's ability to show labels underneath each file (here I show e.g., the caption) gives me all the information I need.

Anyway, what I do really isn't important. What is important is you settle on a naming scheme you like  :)

If search and replace in filenames is just not possible for some reason in PM, then a useful addition to PM would be Functions that could act on variables. In a sense we already have the Substring function in the substring extraction but what about adding more functions like:

1. Length{variable} which would return the length of the string
You may not need this one as you can already offset from the end of a certain string

2. Position{variable,"string"] which would return the position or number of characters from the left of the starting position of "String" in Variable. Then you could use the value of Position to hash into a string.
3. etc
Things like this could indeed be useful. With all the work on the cataloguing feature, however, this one might not get implemented anytime soon (if at all).

Hmm, now I'm thinking about it, your problem could be solved on the command-line much simpler. Simply run the following command in a directory where you have files you need the _IMG part removed off:
for a in *_IMG*; do mv "$a" "${a//_IMG/}"; done
This command will go over all files with _IMG in their name, and rename (mv) them, stripping the _IMG bit. On the filenames you gave as an example this works flawlessly, but if you want to test before you change the filenames, all you need to do is change the mv in above line to echo; this will then just list both the original name as well as the new name (i.e., without the _IMG part).

Hope this solves it for you,
    Hayo
« Last Edit: April 15, 2010, 01:57:34 PM by Hayo Baan »
Hayo Baan - Photography
Web: www.hayobaan.nl