MacMost Q&A Forum • View All Forum QuestionsAsk a Question

How Do I Remove Last 25 Characters Of the File Name Before the Extension?

I would like to rename the files that have time stamp added on the end of the file name.

Basically remove last 25 characters before the extension.

Examples:

IMG523314(2021-12-05-14-51-25_UTC).jpg > IMG523314.jpg
Test run1(2021-08-05-11-32-18_UTC).txt > Test run1.txt
25 characters before the extension (2021-12-05-14-51-25_UTC)

I learn about use of Terminal script and Automator to have it as a Quick action in the right click menu.
However no luck to get it running.

I have File History App files backup on external media from Windows PC that motherboard died.

The File History App can not read the files on the other Windows PC, but I can bring the files to the new computer from the external media. I needed to go thru two external SSD media due to the need for exFAT formatting for Mac. I decided to go with the Mac computer and copied the files to the Mac documents, OK.

However, the File History App time stamp is everywhere. I hope to speed up the process of renaming the latest files.

The Mac Time machine App seems similar to the Windows File History App and creates an identical time stamp on the backup file names. So, this solution may be handy to recover the Time machine App files on external media if some corruption happens.
—–
Dan

Comments: 18 Responses to “How Do I Remove Last 25 Characters Of the File Name Before the Extension?”

    2 years ago

    Basically, you want to remove the parenthesis and everything inside them. That can easily be done with a Shortcut.

    Here's what such a shortcut would look like. It is fairly simple to build. Note that it is set up as a Quick Action with Finder and Services Menu selected. So you would just select the files you want and either trigger the Quick Action or choose Finder, Services, and the name of the Shortcut.

    Then it basically looks at every file, creates a new piece of text that is the name but without the contents of the parenthesis, and then renames the file.

    Dan
    2 years ago

    Hi Gary,

    Thank you for your reply.
    In the Shortcut App, I opened Quick Actions, and click on the + button. and created exactly what you show above. However, it does not show in the Finder right-click Quick Action menu. So I can not test drive it. I am a few days new to Mac and sorry, but not sure how to proceed. i am sure iti is something that I miss.
    Thanks

    2 years ago

    Dan: I wasn't sure how familiar you were with Shortcuts. See https://macmost.com/an-introduction-to-shortcuts-on-the-mac.html for an intro. Make sure you go to the Options screen for the shortcut and enable the Quick Actions and Services like I mentioned.

    Dan
    2 years ago

    Thank you for the great video for an intro to Shortcuts. Yes, after I click on the Shortcut details or the options screen, the Finder checkmark was missing. Yes, now the newly created shortcut name shows on the Right-click menu in the Finder App. I tested and had no joy. I tested on files with (.date stamp..) in the file name and tried different file extensions .txt .ink .jpg no any effect. Is it possible to remote in and see for directly?
    Many Thanks,

    2 years ago

    Dan: Not sure what you mean by "remote in" but just check your Shortcut carefully and review each part to make sure it is right.

    Dan
    2 years ago

    Would you be able to advise where to learn more about \(.*) syntax? I found https://support.apple.com/en-ca/guide/shortcuts-mac/welcome/mac
    but not much help for this syntax.
    Thanks,

    2 years ago

    Dan: That's REGEX syntax, which is pretty hard to understand. Search REGEX and you'll come up with lots (including some 1000-page books on the subject). But for now, check to make sure you have exactly what I have, every single character. That could be where you are going wrong. It is \(.*\) not \(.*) -- there needs to be a backslash before ( and before ) so they are taken literally.

    Dan
    2 years ago

    Sounds good. Thank you for the lead. I am confirming it is \(.*\) in the App. I made typo above. I am keep checking teh code over again. The good news is that it does shows under right click Quick action menu in the Finder. When run it I see the screen kind of blink so it is doing something but not what is expected to do.

    Dan
    2 years ago

    I wonder is the brand new Mac Mini has all the Regex support software including BRE mode as per https://en.wikipedia.org/wiki/Regular_expression
    The BRE mode is required for the syntax \( \)
    Thanks,

    2 years ago

    Dan: Not sure what you are asking there. It doesn't matter which Mac model you have. This is part of the Shortcuts app. The Shortcuts app supports REGEX in several functions, like the Replace function I show. You can even see the checkmark next to the "Regular Expression" option in the screenshot.

    2 years ago

    Dan: If you are still having trouble, maybe post a quick screenshot to show what you have so far using service like Imgur.com or the like.

    Dan
    2 years ago

    Hey Gary, Thank you for confirming REGEX functionality. Yes, I will post a quick screenshots soon. Great idea, Thank you.

    Dan
    2 years ago

    Hey Gary,
    I deleted all my shortcuts attempts and created new named: rename -between-()
    Please see screenshots in the sequence 10 11 12 13 14 15
    it does rename but not as expected. It is grabbing the file body text and placing into the file name.
    https://imgur.com/a/CnSthHM
    Thanks

    2 years ago

    Dan: Ah, I see the problem. Your initial example was an image file, so I built it for that. When you do it with a text file, the "Repeat Item" is the text in the text file, not the name of the text file. So you'll need something more complex that takes the name of the file, not the file itself. You'll need to insert a "Get Details Of Files" action in there. Then make sure that the result of that (Magic Variable) are fed into the Replace function, not the original Repeat Item.

    Dan
    2 years ago

    Thanks Gary, I will test the above soon.
    My initial example were one jpg and one txt file. However, if possible I hope the script program should work for any file .extension.
    I will test drive.
    Thanks for pushing through.

    Dan
    2 years ago

    Thanks for the new script program. It works as expected. Happy here! However, while testing files rename there was a pop-up window titled 'Privacy' asking to confirm the rename by the Quick action. I press Yes or Allow but after the rename does not get done. Works on some files and does not on some other. It seems if I change folders and come back works until hit the next file that does not. I do not think the problem is with the script rather with the System Privacy. Thanks

    Dan
    2 years ago

    To share my Google search on the REGEX syntax above;

    \( & \): Parenthesis are special char's., they signify groups, to match them properly, you need to escape them with backslashes.
    .: Periods are wildcard matcher, they match any single character.
    *: Asterisks are a quantifier, meaning match zero to infinite number of the previous matcher.

    So finally the re-cap;
    Match exactly 1 opening bracket
    Match an unlimited number of any character
    Match exactly 1 closing bracket

    Jesse the K
    2 years ago

    This Mac app helps you create, debug, and use regular expressions:

    RegExRX (where RX stands for "prescription medicine")

    https://apps.apple.com/us/app/regexrx/id498370702?mt=12

Comments Closed.