batch file renaming

Discussion in 'Mac Programming' started by Rmpl, Feb 27, 2009.

  1. macrumors member

    Joined:
    Mar 14, 2007
    #1
    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.
     
  2. macrumors 65816

    Joined:
    Jun 29, 2007
    #2
    Simple Bash or Perl script is how I would do it.
     
  3. jsw
    Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #3
    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.
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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
     
  5. macrumors 65816

    telecomm

    Joined:
    Nov 30, 2003
    Location:
    Rome
    #5
    You could always use Automator too, which probably wins the title "easiest way to do this."
     
  6. macrumors 604

    QuarterSwede

    Joined:
    Oct 1, 2005
    Location:
    Colorado Springs, CO
    #6
    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.
     
  7. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    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
     
  8. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #8
    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.
     

    Attached Files:

  9. thread starter macrumors member

    Joined:
    Mar 14, 2007
    #9
    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.
     
  10. macrumors member

    Joined:
    Oct 9, 2008
    #10
    Thank you! :)
     

Share This Page