3

I have about 3K .pdf documents named to the following filename convention:

BER_0020236-BER_0020293.pdf

the next file in the sequence could be something like

BER_4039464-BER_9384762.pdf (the numbers are nonsequential).

What I need to do is preserve the first part of the filename BER_0020236, while removing the rest and preserving extension .pdf.

I thought I could use "rename" from the right-click options, select replace, and then input -******* with a blank replacement. This doesn't work - perhaps because finder isn't recognizing * as a wildcard? I can remove - easily, but not characters after -.

Is there an extant script somewhere that would do this, or is there something I'm missing from the finder option?

  • I should say in the meantime I found manytricks.com/namemangler which accomplished nicely - however, I'm still looking for an answer here if possible. – Gryph May 10 '18 at 3:28
  • I wrote a python script to do something similar a while ago. It was a pretty easy build, but I don’t know where I saved it... (rather, I think I saved it on my desktop which is currently being transported cross-country) – NoahL May 10 '18 at 4:10
  • Sorry to ask this, but can you please mark the question as answered? I want to put a bounty on a question I have and need the points. Thanks. – MBUST May 15 '18 at 10:26
2

This should do the trick (just edit FolderTemp to something that works):

tell application "Finder"
    set FolderTemp to alias "Folder with all files"
    set allfiles to files of FolderTemp
    repeat with i from 1 to count of allfiles
        set CurrentFile to item i of allfiles
        set filename to name of item i of allfiles
        set x to offset of "-" in filename
        set y to offset of "." in filename
        set NewFileName to characters 1 thru (x - 1) of filename as string
        set Extension to characters y thru -1 of filename as string
        set NewFileName to NewFileName & Extension as string
        set name of CurrentFile to NewFileName
    end repeat
end tell

I hope it helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .