Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Rmpl

macrumors member
Original poster
Mar 14, 2007
68
0
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.
 

jsw

Moderator emeritus
Mar 16, 2004
22,910
44
Andover, MA
One way to do it - and I'm sure there are better ways - is to do the following:
  1. Open Applications->Utilities->Terminal.
  2. 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.
  3. 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
  4. 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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors 65816
Nov 30, 2003
1,387
28
Rome
You could always use Automator too, which probably wins the title "easiest way to do this."
 

QuarterSwede

macrumors G3
Oct 1, 2005
9,887
2,158
Colorado Springs, CO
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors 68000
Feb 1, 2005
1,887
1
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.
 

Attachments

  • Picture 1.png
    Picture 1.png
    31 KB · Views: 153
  • Futil.zip
    55.9 KB · Views: 99
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.