Marius van Witzenburg We fight for our survival, we fight!

14Jun/110

How to batch rename files and use the file its own name content with Bash

Posted by mariusvw

In my example the files had the right content in their name only the position was wrong.

The original files their name were:

The file title - vendor_1.mp3
The file title - vendor_4.mp3
The file title - vendor_22.mp3
The file title - vendor_39.mp3

But afterwards I found this is a difficult way of searching on a vendor. Which I do the most.
So, I decided to make a simple rename script to move the vendor to the beginning of the file name and the title to the end of the filename.

This would give the following filenames as result:

vendor_1 - The file title.mp3
vendor_4 - The file title.mp3
vendor_22 - The file title.mp3
vendor_39 - The file title.mp3

I hope you find this useful or that you can modify it for your own use. You can see the tiny script I used below.

#!/usr/local/bin/bash
 
root="/my/files/location"
 
find $root -type f | while read file
do
    filename=${file##*/}
    piece1=${filename%% - *}
    piece2=${filename##* - }
    extension=${filename##*.}
    newfile="${piece2%%.$extension} - ${piece1}.$extension"
    mv "${file}" "${root}/${newfile}"
done
13Jul/101

Update Mac OS X Finder Workflows to Services, rename files to lowercase

Posted by mariusvw

As some of you might have noticed, the folder actions submenu where you could add your own workflows isn't there anymore in Mac OS X 10.6 (Snow Leopard).
Apple decided to move this to the service menu instead, which is in my opinion a good step since you can now add a service action to almost every program on your Mac.

On my Mac I had a workflow to easily rename all selected files form a UpPeRcaseD name to a lowercase name.
The first thing I noticed when I needed this workflow that it wasn't there... So I looked around a bit and when opening the workflow in Automator it gave an error that it was an old workflow that needs to be updated to a new version.

Since the workflow is quite simple I decided to make a new one. While doing this I found the service option.
Fist I thought I didn't need that but after about 2 minutes I noticed that it was the new way of adding your workflows to Finder or other applications.

Well, lets get started... Fire up Automator!

Create a new service

Search for Finder and drag "Get Selected Finder Items" to the right

You might get this warning, simply click Don't Add

Search for "Rename" and drag "Rename Finder items" to the right

In the above image you can also see the right settings. Change case of the Full name to lowercase.

Save the service

You will end up with this screen, quit Automator

Now your ready to test your service.
Goto a folder where you want to lowercase your files and select the items you want to rename to lowercase. Goto the "Finder" menu and click your newly created service.

Good luck :-)