PDA

View Full Version : batch file renaming




Rmpl
Feb 27, 2009, 10:01 PM
Here is the problem:

I have a lot of files called "File Name With Spaces - S05E06 - Episode Name.avi". I want to remove all the spaces in the filename and replace them with periods. I also want to remove the hyphens.

What is the easiest way to do this? Thanks.



detz
Feb 27, 2009, 10:04 PM
Simple Bash or Perl script is how I would do it.

jsw
Feb 27, 2009, 10:24 PM
One way to do it - and I'm sure there are better ways - is to do the following:

Open Applications->Utilities->Terminal.
Go to the directory with all of your AVI files. The easiest way to do this is to, in Terminal, type "cd " (minus the quotes) and then to drag the folder containing the AVI files onto Terminal, which will complete the command. You'll end up seeing something like "cd /Users/me/MyMovies". Hit the return/enter key to move there.
Enter the following things (you can cut and paste each line, hitting 'return' after each):

tcsh
foreach file ( *.avi )
mv $file `echo $file | sed 's/ /./g' | sed 's/-//g' | sed 's/.././g'`
end

Type control-D

You might want to do this on a copy of the directory, just to be safe. Here's what the commands do:

The "cd ..." command tells Terminal to change to the specified directory.

The "tcsh" command tells Terminal to use the tcsh shell. Others will work fine; these commands work at least with tcsh.

The "foreach file ( *.avi )" command tells the shell to gather a list of all files ending in avi.

The "mv $file `echo $file | sed 's/ /./g' | sed 's/-//g'`" command does a number of things:


The "echo $file" part basically echoes the name of the file (i.e., "File Name With Spaces - S05E06 - Episode Name.avi")
The "sed 's/ /./g'" part replaces all spaces with periods (i.e., "File.Name.With.Spaces.-.S05E06.-.Episode.Name.avi")
The "sed 's/-//g'" part replaces all hyphens with nothing (i.e., "File.Name.With.Spaces..S05E06..Episode.Name.avi")
The "sed 's/.././g'" part replaces all double periods with periods (i.e., "File.Name.With.Spaces.S05E06.Episode.Name.avi")
The "mv" command renames the file to the new name you've just constructed.

The "end" command tells the script that you're done entering it.
The control-D at the end exits you out of the tcsh shell.

lee1210
Feb 27, 2009, 10:26 PM
This is certainly not the best, and you should back up your files before you test anything like this, but you can try, from the directory containing the files to rename:
ls | awk '{printf "mv '\''"$0"'\'' ";gsub(/-/,"");gsub(/ +/,".");print $0}' | sh

it removes the - first, then takes any number of spaces and turns them in to one .. for the mv command, the original filename is quoted because it will have spaces. the new one is not, since they are being removed.

Again, i'm sure others can come up with much more beautiful things, but i believe this will work.

-Lee

telecomm
Feb 28, 2009, 12:21 AM
You could always use Automator too, which probably wins the title "easiest way to do this."

QuarterSwede
Feb 28, 2009, 12:25 AM
Why go through all that trouble when you can just use Name Mangler?

Sorry for no link. You can thank Apple for not including copy & paste on the iPhone. Just Google it.

lee1210
Feb 28, 2009, 12:48 AM
Why go through all that trouble when you can just use Name Mangler?

Sorry for no link. You can thank Apple for not including copy & paste on the iPhone. Just Google it.

What do you learn doing THAT? =)

Google-fu and finding a utility to do what you want is a valuable skill, but putting together your own solution is more rewarding, IMO.

-Lee

Soulstorm
Feb 28, 2009, 06:00 AM
Some time ago (when I was 18) I made a very popular (back then) applescript application, which has all those functions you need. It is a droplet. YOu open it with a double-click, and you choose the function you want. The application will then quit. After that, drop the files you want on it, and it will perform the selected function.

Maybe you should check it out. I am attaching the application with the read-me.

Rmpl
Mar 1, 2009, 07:44 AM
Why go through all that trouble when you can just use Name Mangler?

Sorry for no link. You can thank Apple for not including copy & paste on the iPhone. Just Google it.

Ding ding ding. We have a winner. Thank you.

http://www.manytricks.com/namemangler/

Thanks to everyone else too. All the other posts were very informative.

Peter Maurer
Mar 1, 2009, 09:37 AM
Ding ding ding. We have a winner.

Thank you! :)