MacMusic  |  PcMusic  |  440 Software  |  440 Forums  |  440TV  |  Zicos
word
Recherche

Complex Batch File Renaming With Shortcuts and Regular Expressions

vendredi 21 juillet 2023, 17:00 , par MacMost
If the Finder's batch rename tool can't do what you want, you can always create your own complex file renaming scripts using the Shortcuts app and regular expressions.


Video Transcript: Hi, this is Gary with MacMost.com. Let me show you how you do complex batch file renames using the Shortcuts App.
MacMost is brought to you thanks to a great group of more than 1000 supporters. Go to MacMost.com/patreon. There you can read more about the Patreon Campaign. Join us and get exclusive content and course discounts.
Now the Batch Rename tool in the Finder is useful for simple replacements. You can replace one group of characters with another group of characters. But if you want to do something more complex you would have to go to the Shortcuts App. You can do a lot with the Shortcuts App including using regular expressions.
So I've got a bunch of files here that I can rename. Let's go to the Shortcuts App and create a new shortcut. Let's do an example here. Something pretty simple. Like let's look for file names that have three words in them and then reverse the order of the words. So that's definitely not something we can do with the regular Batch Rename tool. But here if we wanted to do it in Shortcuts what we would start with first is we would go to Details, here on the right, and we'd say let's use this as a Quick Action and in the Finder. That allows us to select a bunch of files and then execute a Quick Action. This Quick Action that we're creating. Let's call this Rename File: Reverse Three words.
Now let's go and have it just receive files. So I'll click here and then I'll turn all these Off. The quick way to do that is to Select All, deselect all and now just Add to Files. So now we see Files from Quick Actions. There's nothing. Let's just say Stop and Respond with no response so it just doesn't run if no files were selected.
Now we want to go to the actions here. We want to loop through all the files that were selected. So I'm going to do a repeat loop and I'll Repeat with Each. I'll drag that in. So, all the files that come into the Shortcut Input is going to repeat and go through them. Now we need to get the name of the file. To do that we'll look for Details, Files, and we have Get Details of Files. We want to add this here twice actually. Let's put it inside the Repeat Loop. The second time inside the Repeat Loop too.
The first detail we'll get is we'll get the file extension. We'll get the extension of the repeat item. Each time through the loop it gets the file extension. The next one we'll get is the file name and we'll get that of the repeat extension as well so it will clear that out. Click here and say the Repeat Item. So now with the extension we get the name. Now what we want to do is process the name. So let's go and Replace, and there's Replace Text right here, so we'll use that and if we look at Show More we can see one of the things we can do with Replace Text is use a Regular Expression.
So this gives us access to a complete language about how to handle text. Regular Expressions are really complex things but you can look up all sorts of information online about how to use Regular Expressions for various things.
We're going to do something fairly simple here. So we're going to look for a pattern and it is going to start with the caret symbol, Shift 6 on US keyboards. This means the beginning of the text. Then we're going to put in parentheses the first word. So, we can do this many different ways. I'm going to choose Backslash W for word characters and asterisk for as many word characters as needed. Close the parentheses. Then I'm going to do Backslash and then S for whitespace and then the second word, like that, space and then third word and then a dollar sign to indicate the end of the pattern. So the pattern has to be word space word space word. Each of the words is captured. It's part of what is called a Captured Group. You've got this in parentheses. So everything in parentheses, this is one, this is two, this is three, is captured. So the output can take those captured bits and reuse them. So I can do dollar sign 3 for the third word, space, dollar sign 2 for the second word, space, $1 for the first word.
So it is going to replace word 1, word 2, word 3 with word 3, word 2, word 1 spaces in-between them. Then it will make sure we have case sensitive turned Off and we should now have valid names that come out of this where it reverses those three words. Let's take Rename File and put that as the next item. Then we're going to rename the repeat item with, and we'll go in here and we can put the output from this. So I'll Control Click and insert variable and say the updated text dot and then Control Click insert variable and the final extension that we got right here.
Now, it should go through. Loop through all of the selected files. Take first word, second word, third word and then reverse them, replace it, and now it's going to rename that file with this new name.
Another problem we're going to run into is that if I try to rename a file with the same thing, like the second one here doesn't match a pattern so it's going to try to rename beach.jpeg as beach.jpeg. It's going to give me an error. Shortcuts isn't smart enough to know just to skip it. So what we want to do here is we're going to do an IF statement and we're going to say, IF the updated text is the original name, so it hasn't changed, then do nothing. Otherwise rename the file. So only rename the file if there has been a change. So let's go and give this a try here. I'll just go to Quick Actions, Rename, and you could see it will go in and the two files that had three words in them, the words in them are now reversed.
So this is a simple example to show you how you could basically use any Regular Expression here using the replaced action to perform really complex changes on file names based on a pattern. If you're good with Regular Expressions then you can use this to do just about anything. You could also, of course, use Regular Expressions in Terminal but if you prefer to have it here in Shortcuts then you can do it here and also, of course, this is reusable very easily. So you can have this setup and if this is some sort of pattern that you need to match and change in File Names often then you just have this shortcut here ready to go whenever you need it.
Hope you found this useful. Thanks for watching. Related Subjects: Shortcuts (47 videos)
Related Video Tutorials:
Creating Regular Repeating Reminders ― macOS Shortcuts: Add a Watermark To an Image ― macOS Shortcuts: Capture Text From Your Screen ― How To Batch Process Images With Mac Preview
https://macmost.com/complex-batch-file-renaming-with-shortcuts-and-regular-expressions.html
News copyright owned by their original publishers | Copyright © 2004 - 2024 Zicos / 440Network
Date Actuelle
dim. 28 avril - 11:09 CEST