How can I automatically remove characters from 200 filenames?
January 23, 2005 7:13 PM   Subscribe

Six characters, in search of a way to replace them
I have a series of files whose names I'd like to edit by taking out part of the name and replacing it but the part I'd like to remove from each file name is different. [more]

I have more than 200 files whose names look like this:
filename.garbage.000
filename.garbage.001
filename.garbage.002
etc.
In each file, the garbage I'd like to remove is six characters between the two dots, and there seems to be some kind of sequence to them (#3E3A0, #3E3A1, #3E3A2, etc.) but they all need to go. I guess one of the dots needs to go, too. Since the garbage in each file is different, that screws up the ordering of the part numbers at the end of the filenames and we can't have that.

I'm using OS X and I've tried the various filename-editing Applescripts I have, but those haven't done what I wanted them to. (Unless I was doing it wrong.) Any Applescript or Terminal gods out there?
posted by emelenjr to Computers & Internet (16 answers total)
 
Response by poster: And, I should have mentioned, it would be best if the garbage could be replaced by a filename extension. Thank you to anyone willing to guide me.
posted by emelenjr at 7:15 PM on January 23, 2005


This Perl script looks like it should do the trick. My regexp skills are a bit rusty so I'm not sure what arguments you'll need to feed the script, but there are tons of resources available for that.
posted by sanko at 7:32 PM on January 23, 2005


Check out flexible renamer for what looks to be a pretty beefy GUI for this sort of thing. I've only used it a couple times but it looks like there's probably 10 different ways it could solve your problem.
posted by Wood at 7:39 PM on January 23, 2005


That's a windows program. Darn, reading comprehension not what it should be. Oh well, my link might help someone (if it weren't for the fact that I probably found here on ask me anyway.)
posted by Wood at 7:40 PM on January 23, 2005


I'd also look into A Better Finder Rename. It's for exactly this type of thing.
posted by Jeff Howard at 7:40 PM on January 23, 2005


For OSX.
posted by Jeff Howard at 7:41 PM on January 23, 2005


Response by poster: I'll look into the Per scripts, but Flexible Renamer is a Windows app, which I can't use. Thanks for looking, though. I'm checking Versiontracker for my options there, too.

On preview, thanks Jeff. And thanks for the effort anyway, Wood.
posted by emelenjr at 7:43 PM on January 23, 2005


Awk can be used for this sort of thing, I assume macs have it available in the terminal window. Lots of Awk scripting examples on the web.
posted by 445supermag at 8:26 PM on January 23, 2005


Sorry this doesn't help the original poster, but if anyone's looking for a tool for Windows, this one is by far the best I've found.
posted by stavrosthewonderchicken at 8:47 PM on January 23, 2005


Welcome to Unix! Here what you want (you type the bold bits):

% foreach f ( filename.* )
foreach? mv $f $f:r:r.$f:e
foreach? end

What's happening here? tcsh is the default shell under OS X, and "foreach" is the tcsh loop operator. (The "%" in the first line represents the prompt; don't type that.) So the first line is you telling the shell to loop through all files in the current directory whose names start with "filename.".

The next line is foreach's prompt followed by your telling the shell what you want to do to each of those filenames. "mv" is the Unix "move" or "rename" command. "$f" refers back to the "f" variable you told the shell to assign to each filename. ":r" means "the root" - that is, take the extension off the filename. We do it twice to get rid of both the ".001" and the ".garbage". Then we add back on a "." and the "001" extension, which we get using ":e".

Once we enter "end" the "foreach" command executes and renames the files.
posted by nicwolff at 9:48 PM on January 23, 2005


Response by poster: Thanks for explaining that, nicwolff. From your example, I think I understand what to do in my case. But I am lazy, so I downloaded A Better Finder Rename. And I am cheap, so I used the program unregistered to handle renaming the files ten at a time. Next time I need to do something like that, I'll give your method a shot.
posted by emelenjr at 11:27 PM on January 23, 2005


Just a note for future reference, nicwolff, but tcsh has been pre-empted in OSX 10.3+ by the good old almost-all-Linux-distros-have-it-as-the-default-shell 'bash' instead. FYI =) I don't know if bash has foreach but it does have 'for [variable name] in [list] do [command list]; done' which works fairly well.
posted by cyrusdogstar at 10:53 AM on January 24, 2005


New accounts you create under 10.3 will have bash as the default shell, but accounts that already existed when you upgraded will still have tcsh as their shell if it hasn't been changed.
posted by kindall at 1:00 PM on January 24, 2005


(Nicwolff and stavros, thanks to both of you for that guide and link, respectively.)
posted by kchristidis at 1:36 PM on January 24, 2005


Thanks for the heads-up, cyrus and kindall. emelenjr, of course you can just start a tcsh shell by typing "tcsh" if you want to use my shell trick unmodified - bash has "for...in..." but I'm not sure how it does ":r" and ":e".
posted by nicwolff at 2:01 PM on January 24, 2005


Using File Renamer would probably do this pretty easily.
posted by stoneegg21 at 9:19 PM on January 25, 2005


« Older Parenting impulsive kids   |   I'm trying to convert cassette tapes to MP3s... Newer »
This thread is closed to new comments.