batch file renaming

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

  1. Rmpl macrumors member

    Mar 14, 2007
    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. detz macrumors 65816

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


    Mar 16, 2004
    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.
  4. lee1210 macrumors 68040


    Jan 10, 2005
    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.

  5. telecomm macrumors 65816


    Nov 30, 2003
    You could always use Automator too, which probably wins the title "easiest way to do this."
  6. QuarterSwede macrumors G3


    Oct 1, 2005
    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.
  7. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    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.

  8. Soulstorm macrumors 68000


    Feb 1, 2005
    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. Rmpl thread starter macrumors member

    Mar 14, 2007
    Ding ding ding. We have a winner. Thank you.

    Thanks to everyone else too. All the other posts were very informative.
  10. Peter Maurer macrumors member

    Oct 9, 2008
    Thank you! :)

Share This Page