homeGeek CultureWebstoreeCards!Forums!Joy of Tech!AY2K!webcam

The Geek Culture Forums


Post New Topic  New Poll  Post A Reply
my profile | directory login | | search | faq | forum home
  next oldest topic   next newest topic
» The Geek Culture Forums   » Other Geeky Stuff   » Ask a Geek!   » Deleting characters from Mac filenames (Page 1)

 - UBBFriend: Email this page to someone!  
This topic comprises 2 pages: 1  2 
 
Author Topic: Deleting characters from Mac filenames
Moe Monkey
Geek
Member # 1900

Icon 1 posted April 15, 2006 08:30      Profile for Moe Monkey     Send New Private Message       Edit/Delete Post   Reply With Quote 
Okay, I'm missing something, 'coz this should be easy.

Current operating system is Mac OS 10.4.6.

I have a large number (hundreds) of sequencially numbered files from which I would like to strip the number. The number is the first four characters of each file name.

I have examined the rename options that File Buddy, Automator, and iView Media Pro offer, as well as the "mv" command... I can't see anything that lets me do what I want to do. I've done a Google search, but with no luck. Either I'm using the wrong search terms or I am not recognizing the solution in front of me.

I'm convinced this is possible -- what am I missing?

Moe

Posts: 145 | From: The couch in the living room | Registered: Dec 2002  |  IP: Logged
Snaggy

Sir Snaggalot!
Member # 123

Member Rated:
5
Icon 5 posted April 15, 2006 09:07      Profile for Snaggy   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
Did you try A Better Finder Rename?

http://www.publicspace.net/ABetterFinderRename/

(edit: hmmm, looks like you have to pay for that though. The trial version of the program is limited to renaming 10 files at a time.)

Posts: 8100 | From: Canada | Registered: Jan 2000  |  IP: Logged
Bibo
BlabberMouth, the Next Generation
Member # 1959

Member Rated:
4
Icon 1 posted April 15, 2006 09:30      Profile for Bibo   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
What kind of files are these? If they are image files you can batch rename them in Photoshop 7 using the file browser. Or if you have any of the latest Adobe CS applications or PS Elements you can batch rename Adobe Bridge.
Posts: 1641 | From: Grand Rapids, MI | Registered: Jan 2003  |  IP: Logged
Moe Monkey
Geek
Member # 1900

Icon 1 posted April 15, 2006 09:39      Profile for Moe Monkey     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by Snaggy:
Did you try A Better Finder Rename?

I actually had a paid version of ABFR back in my OS 9 days, but never upgraded... I thought FileBuddy etc. could take care of everything. I'll take another look, though.
Posts: 145 | From: The couch in the living room | Registered: Dec 2002  |  IP: Logged
Moe Monkey
Geek
Member # 1900

Icon 1 posted April 15, 2006 09:49      Profile for Moe Monkey     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by Bibo:
What kind of files are these? If they are image files you can batch rename them in Photoshop 7 using the file browser.

I have Photoshop Elements, the batch rename function doesn't seem to do what I want. All of these things can alter the extension, but not, apparently, a prefix (unless it's the same prefix for all files, which is not the case).
Posts: 145 | From: The couch in the living room | Registered: Dec 2002  |  IP: Logged
dragonman97

SuperFan!
Member # 780

Member Rated:
4
Icon 1 posted April 15, 2006 10:06      Profile for dragonman97   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
I'm missing something as well - if you strip number from the file, what will make its name unique? Is there something else distinguishing about them?

Personally, I'd probably write a 5 line Perl script to do it. [Big Grin] I know it can be done with sh, sed & awk, but I admit being more comfortable with Perl.

#!/usr/bin/perl -w
use strict;

my @files=<*.something>; #insert correct glob here
foreach(@files){
my $newfn=substr($_, 4); #or something more appropriate to your case (possibly RegEx).
system("mv", $_, $newfn);
}

--------------------
There are three things you can be sure of in life: Death, taxes, and reading about fake illnesses online...

Posts: 9331 | From: Westchester County, New York | Registered: May 2001  |  IP: Logged
Moe Monkey
Geek
Member # 1900

Icon 1 posted April 15, 2006 10:57      Profile for Moe Monkey     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by dragonman97:
I'm missing something as well - if you strip number from the file, what will make its name unique? Is there something else distinguishing about them?

The filenames without the numbering are unique, the numbering was added to provide another sort option.

quote:
Originally posted by dragonman97:
Personally, I'd probably write a 5 line Perl script to do it.

...and that's when I lost consciousness... [Big Grin] Actually, this is more in the spirit of what I'd like to do, except that I'm not a programmer and so I might have to brush up a bit on the particular commands. It looks like you are assuming that the end of the file name is constant, which it isn't. What I'm trying to do is turn (001_Alice, 002_Bob, 003_Chris) into (Alice, Bob, Chris).

Snaggy: A Better Finder Rename definitely does the trick, but I'm reluctant to shell out $20 US if my computer already has the basic capability I'm looking for. Kind of like buying Word and then discovering that TextEdit suits my needs. If this situation was a more common occurrance, I'd go for it instead of spending too much time wringing my hands!

Thanks to all for the help so far.

Posts: 145 | From: The couch in the living room | Registered: Dec 2002  |  IP: Logged
sosumi
Uber Geek
Member # 1106

Member Rated:
5
Icon 1 posted April 15, 2006 12:38      Profile for sosumi   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
Name Munger has a "strip characters" tab, enter _0123456789 in "characters to be removed" and they'll all go away.

It's multi-platform, so scroll down for the Mac version. The download is an evaluation copy, but it's fully functional and if you decide to keep it, it's only $10. The site doesn't say whether it will run under 10.4, but it does work in panther.

It's a lot simpler than batch rename in PS, if only for the sake that it's drag and drop and you don't have to go hunting for it.

Posts: 845 | From: Boston | Registered: Feb 2002  |  IP: Logged
Metasquares
Highlie
Member # 4441

Member Rated:
5
Icon 1 posted April 15, 2006 15:27      Profile for Metasquares   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
All you should need to do then is modify the glob. Are they all in one directory?
Posts: 664 | From: Morganville, NJ | Registered: Oct 2005  |  IP: Logged
drunkennewfiemidget
BlabberMouth, a Blabber Odyssey
Member # 2814

Member Rated:
4
Icon 1 posted April 15, 2006 16:03      Profile for drunkennewfiemidget     Send New Private Message       Edit/Delete Post   Reply With Quote 
code:
for i in * ; do mv $i `echo $i | sed "s/[[:digit:]]//g"` ; done

And dman: perl does have a rename() function that does what mv does (but cant go across filesystems, since it actually renames files..)
Posts: 4897 | From: Cambridge, ON, Canada | Registered: Jun 2004  |  IP: Logged
ASM65816
SuperBlabberMouth!
Member # 712

Member Rated:
2
Icon 4 posted April 15, 2006 16:56      Profile for ASM65816   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
In AppleScript:
code:
 
-- Strip Leading Numbers and "_" from Selected Files
-- If " 001_Monkey.jpg" is selected, it's renamed to "Monkey.jpg"
-- WARNING: This is a weak hack and should be a droplet, but I've got
-- a headache. Enter this in "Script Editor", Compile, and Run....
-- WARNING #2: No Error Detection for Duplicate File Names !!!!

tell application "Finder"
set selectedItems to selection
set itemCount to count selectedItems
repeat while itemCount > 0
set oldName to name of item itemCount of selectedItems
set newName to oldName
set myFolder to folder of item itemCount of selectedItems
set trimChars to " _0123456789"
set ch to character 1 of newName
set n to offset of ch in trimChars
repeat while n is not equal to 0
set n to count newName
if n > 2 then
set newName to text 2 thru n of newName
set ch to character 1 of newName
set n to offset of ch in trimChars
else
exit repeat
end if
end repeat
if newName is not equal to oldName then
set name of document file oldName of myFolder to newName
end if
set itemCount to itemCount - 1
end repeat
end tell

I'll probably try to find some references to do the job "right" as a Droplet later. [ohwell]

--------------------
Once a proud programmer of Apple II's, he now spends his days and nights in cheap dives fraternizing with exotic dancers....

Posts: 1035 | From: Third rock from sun. | Registered: Mar 2001  |  IP: Logged
The Famous Druid

Gold Hearted SuperFan!
Member # 1769

Member Rated:
4
Icon 1 posted April 15, 2006 17:35      Profile for The Famous Druid     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by dragonman97:
Personally, I'd probably write a 5 line Perl script to do it. [Big Grin] I know it can be done with sh, sed & awk, but I admit being more comfortable

5 lines?
Looxyery!
Wen aye were a lad, we ad t' lick teletype clean wi' toong.


<disclaimer>totally untested</disclaimer>

code:
ls *.jpg | gawk '{printf "mv %s %s\n", $0, substr($0,4) }' | bash 



--------------------
If you watch 'The History Of NASA' backwards, it's about a space agency that has no manned spaceflight capability, then does low-orbit flights, then lands on the Moon.

Posts: 10668 | From: Melbourne, Australia | Registered: Oct 2002  |  IP: Logged
Moe Monkey
Geek
Member # 1900

Icon 1 posted April 15, 2006 17:41      Profile for Moe Monkey     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by sosumi:
Name Munger has a "strip characters" tab, enter _0123456789 in "characters to be removed" and they'll all go away.

I checked it out but the utilities I have can already do this. The problem is that this particular solution would also remove numbers elsewhere in the name. I'm looking for the ability to remove a set number of characters from the beginning of the file name and nowhere else.

Anyway, thanks for the link. If nothing else, I got a chuckle out of the different "Download" buttons. I'm easily amused. [Smile]

Posts: 145 | From: The couch in the living room | Registered: Dec 2002  |  IP: Logged
Moe Monkey
Geek
Member # 1900

Icon 1 posted April 15, 2006 17:58      Profile for Moe Monkey     Send New Private Message       Edit/Delete Post   Reply With Quote 
Wow, lots of posting while I was off composing replies.

code:
 for i in * ; do mv $i `echo $i | sed "s/[[:digit:]]//g"` ; done 

and
code:
 ls *.jpg | gawk '{printf "mv %s %s\n", $0, substr($0,4) }' | bash  

look intriguing (thanks DNM and TFD -- didn't know you were from Yorkshire [Applause] ), care to break these down and explain what they mean? You can tell me to jump in a lake if you don't feel like tutoring basic programming. (I guess this isn't really programming, just showing my ignorance!)

The Applescript from ASM65816 I understand -- might take a while to chug through 500 files, though! [Smile]

Thanks again for everyone's indulgence. At this stage, I feel a bit guilty for asking since the value of the effort expended probably surpasses the $20 for ABFR. Hopefully you're having fun with this.

Posts: 145 | From: The couch in the living room | Registered: Dec 2002  |  IP: Logged
The Famous Druid

Gold Hearted SuperFan!
Member # 1769

Member Rated:
4
Icon 1 posted April 15, 2006 18:17      Profile for The Famous Druid     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by Moe Monkey:
care to break these down and explain what they mean?

3 unix commands, with the '|' used to send the output from one to the input of the next.

Step 1: ls *.jpg
Simply produces a list of all the files we're interested in (I assumed they were .jpg files)
The only tricky bit here is that you need to know that while ls would default to multi-column output when used interactively, when the output is redirected (using '>' or '|' ) it defaults to one file per line.

step 2: gawk '{printf "mv %s %s\n", $0, substr($0,4) }'
Uses gawk (the gnu version of awk) to process each line of output from step 1.
$0 in the above is the input string (i.e. the file-name output line from step 1)
We use the substr() function to strip off the first 4 characters of the file name, and printf to create a command line to do the rename.


step 3: | bash
uses the bash shell to execute the command line created at step 3

--------------------
If you watch 'The History Of NASA' backwards, it's about a space agency that has no manned spaceflight capability, then does low-orbit flights, then lands on the Moon.

Posts: 10668 | From: Melbourne, Australia | Registered: Oct 2002  |  IP: Logged
drunkennewfiemidget
BlabberMouth, a Blabber Odyssey
Member # 2814

Member Rated:
4
Icon 1 posted April 15, 2006 19:18      Profile for drunkennewfiemidget     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by Moe Monkey:

code:
 for i in * ; do mv $i `echo $i | sed "s/[[:digit:]]//g"` ; done 


for i in *; do

Go through every file that matches the * glob (everything except hidden files), and do the following, replacing $i with every file in succession.

So if I went:
code:
for i in * ; do cat $i ; done

It would go through every file, and cat it. Probably not a good idea -- especially if you have lots of binary files in the dir.

mv $i `echo $i | sed "s/[[:digit:]]//g"`

Run the command mv with parameters $i (filename) and the result of echo $i | sed "s/[[:digit:]]//g" which basically echos the filename, and pipes it to sed, which will replace any digit ([:digit:] character class) with nothing (//) and the g at the end means, 'replace them all'.. without it, it'd remove the first digit, and leave the rest intact.

So for each file matched by *, it will

mv <file> `echo <file> | sed "s/[[:digit:]]//g"`

Which say, if there was only one file named 'blah2345.jpg' it would:

mv blah2345.jpg `echo blah2345.jpg | sed "s/[[:digit:]]//g"`

Which becomes:

mv blah2345.jpg blah.jpg

Which performs the function you were looking for (I think.)

And as you can see by all the answer given, which're all equally valid, there's always more than one way to do it.

Posts: 4897 | From: Cambridge, ON, Canada | Registered: Jun 2004  |  IP: Logged
Moe Monkey
Geek
Member # 1900

Icon 1 posted April 15, 2006 19:33      Profile for Moe Monkey     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by The Famous Druid:
...3 unix commands...

Cool -- I played with this a bit and found something that worked:
code:
 ls * | awk '{printf "mv %s %s\n", $0, substr($0,5) }' | bash 

...bash didn't recognise gawk, and using substr($0,5) strips the first four characters. I want everything in the directory renamed, hence ls * (feels slightly dangerous, hope it isn't...)

(edit: Okay, I just realized that ls as opposed to ls * is good enough...)

Posts: 145 | From: The couch in the living room | Registered: Dec 2002  |  IP: Logged
Moe Monkey
Geek
Member # 1900

Icon 1 posted April 15, 2006 19:34      Profile for Moe Monkey     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by drunkennewfiemidget:

mv blah2345.jpg blah.jpg

Which performs the function you were looking for (I think.)

No, I'm not really looking for something that strips digits specifically, just the first four characters in each file name, digits or letters. Thanks for the explanation, though -- I'm learning a few interesting tricks, here.
Posts: 145 | From: The couch in the living room | Registered: Dec 2002  |  IP: Logged
littlefish
BlabberMouth, a Blabber Odyssey
Member # 966

Member Rated:
4
Icon 1 posted April 16, 2006 01:15      Profile for littlefish   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
It seems like the sort of thing that automator should be able to handle, and it was meant to be designed for non-programmers to use. I can't find any information on wild card substitution though.
Posts: 2421 | From: That London | Registered: Nov 2001  |  IP: Logged
Moe Monkey
Geek
Member # 1900

Icon 1 posted April 16, 2006 12:16      Profile for Moe Monkey     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by littlefish:
It seems like the sort of thing that automator should be able to handle...

It can -- I've just discovered that the Automator Automator library contains an action called "Run Shell Script." I'm trying to be good and get some real work done this weekend, so I'll look into it when I need a break from my marking. I'm assuming that I can jigger it to include commands along the lines that DNM and TFD suggested.
Posts: 145 | From: The couch in the living room | Registered: Dec 2002  |  IP: Logged
drunkennewfiemidget
BlabberMouth, a Blabber Odyssey
Member # 2814

Member Rated:
4
Icon 1 posted April 16, 2006 12:53      Profile for drunkennewfiemidget     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by Moe Monkey:
quote:
Originally posted by The Famous Druid:
...3 unix commands...

Cool -- I played with this a bit and found something that worked:
code:
 ls * | awk '{printf "mv %s %s\n", $0, substr($0,5) }' | bash 

...bash didn't recognise gawk, and using substr($0,5) strips the first four characters. I want everything in the directory renamed, hence ls * (feels slightly dangerous, hope it isn't...)

(edit: Okay, I just realized that ls as opposed to ls * is good enough...)

Actually, ls and ls * mean two very different things.

ls means 'give me a listing of everything in this directory'.

ls * means 'go through every item in this directory and list it'.

The difference being that ls * will even list the contents of directories in this directory, since the shell will expand * before running ls.

Posts: 4897 | From: Cambridge, ON, Canada | Registered: Jun 2004  |  IP: Logged
dragonman97

SuperFan!
Member # 780

Member Rated:
4
Icon 1 posted April 16, 2006 14:42      Profile for dragonman97   Author's Homepage     Send New Private Message       Edit/Delete Post   Reply With Quote 
I still say my Perl is a hell of a lot more legible. [Wink]

Load a list with the file names, iterate over them, copy the old name to a new one, keeping everything past the 4th character (0-based-indexing), and running the 'mv' command to rename from the old name to the new. *Easy.*

*grumbles at the prejudice against Perl.* It doesn't /have/ to be line-noise, ya know?

--------------------
There are three things you can be sure of in life: Death, taxes, and reading about fake illnesses online...

Posts: 9331 | From: Westchester County, New York | Registered: May 2001  |  IP: Logged
Moe Monkey
Geek
Member # 1900

Icon 1 posted April 16, 2006 14:47      Profile for Moe Monkey     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by drunkennewfiemidget:
Actually, ls and ls * mean two very different things.
ls means 'give me a listing of everything in this directory'.
ls * means 'go through every item in this directory and list it'.

Ah, okay -- I didn't notice the diffference because there were no other folders within the one being scanned. Good to know.

On the subject of Automator, I managed to make a droplet that does what I want. I'll double check the syntax to make sure I'm using "ls" instead of "ls *"!

Thanks, everyone, for the help! Maybe it's time to put this one to bed. [Smile]

Posts: 145 | From: The couch in the living room | Registered: Dec 2002  |  IP: Logged
Danapoppa
Geek
Member # 1555

Member Rated:
5
Icon 1 posted April 16, 2006 22:55      Profile for Danapoppa     Send New Private Message       Edit/Delete Post   Reply With Quote 
Just thought I'd add my two cents because my favorite tool was not mentioned.

After looking at a lot of file renaming programs I settled on Renamer4Mac. In my opinion this had the best user interface, and I'm a sucker for a well-designed interface.

Not sure it can do what Moe wants to do, tho.

Posts: 237 | From: South of Mt. Fuji | Registered: Jul 2002  |  IP: Logged
Moe Monkey
Geek
Member # 1900

Icon 1 posted April 17, 2006 08:37      Profile for Moe Monkey     Send New Private Message       Edit/Delete Post   Reply With Quote 
quote:
Originally posted by Danapoppa:
After looking at a lot of file renaming programs I settled on Renamer4Mac.

I'm glad you jumped in, Danapoppa -- I hadn't heard of that one, and it does do what I was looking for. The price is right, too -- donationware via PayPal.

Good timing, I've discovered that my little Automator app chokes if the filename has any spaces in it. I know that in the command line, putting quotes around the whole file name solves this, but how does it work with the following?
code:
 ls  | awk '{printf "mv %s %s\n", $0, substr($0,5) }' | bash  

The obvious newbie answer would be to put quotes around occurances of $0 -- This felt really wrong, but I tried it just to make sure that it doesn't work. I was right, it was wrong. (Where's the Groucho emoticon when you need it!)
Posts: 145 | From: The couch in the living room | Registered: Dec 2002  |  IP: Logged


All times are Eastern Time
This topic comprises 2 pages: 1  2 
 
Post New Topic  New Poll  Post A Reply Close Topic    Move Topic    Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:

Contact Us | Geek Culture Home Page

2015 Geek Culture

Powered by Infopop Corporation
UBB.classicTM 6.4.0



homeGeek CultureWebstoreeCards!Forums!Joy of Tech!AY2K!webcam