Batch remove specific info from filename in 3K .pdf files while preserving remaining info

applescriptfinderpdfrenamescript

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?

Best Answer

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.