Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rate Thread Display Modes
Old 08-17-2004, 04:57 PM   #1
retromango
Prospect
 
Join Date: Aug 2004
Posts: 3
renaming files

Ok, I'm not sure if this is the best place to post this question but here goes:
I would like to write a script that will take a group of sequentially numbered files and change the file names into the corresponding letter. For instance, "file 1" would become "file a" ; "file 15" would convert to "file o" ,etc. I've seen and used scripts that can add sequential numbers to a folder of files and scripts that can replace text, but it makes no sense to run the replace command for each letter of the alphabet. The numbers are guranteed to be under 26. Any clues on how I can achieve this?
retromango is offline   Reply With Quote
Old 08-17-2004, 11:46 PM   #2
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
Here's a gross one (long) liner that will do what you want: change to the dir in which the files reside and issue...

perl -e '@hash{1..26**2+26}=a..zz;foreach $f(@ARGV){next unless (-f $f and ($g=$f)=~s/(\d+)$/$hash{$1}/);next if (-f $g); rename($f,$g)}' *

That'll do for a sequence of up to 702 sequential files. (file 1, ..., file 702).
Command prior to first semi-colon maps numbers to letters. Then, for each entry in the directory (move on unless the file exists and unless it ends with a number), substitute the number for the appropriate letter. Ignore things if a file with the proposed name exists already: otherwise rename the thing.

Obviously you'll want to make sure it does what you're after before letting it loose on anything too precious. (Note: if it does meet files with numbers beyond 702 it'll just strip the ending off, or ignore the file if a "stripped" version already exists. That is, it'll emerge unscathed.)

Cheers,
Paul
pmccann is offline   Reply With Quote
Old 08-18-2004, 12:15 AM   #3
honestpuck
Major Leaguer
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 256
Quote:
Originally Posted by retromango
Ok, I'm not sure if this is the best place to post this question but here goes:
I would like to write a script that will take a group of sequentially numbered files and change the file names into the corresponding letter. For instance, "file 1" would become "file a" ; "file 15" would convert to "file o" ,etc. I've seen and used scripts that can add sequential numbers to a folder of files and scripts that can replace text, but it makes no sense to run the replace command for each letter of the alphabet. The numbers are guranteed to be under 26. Any clues on how I can achieve this?

well some Perl does the trick

Code:
#!/usr/bin/perl

$start = ord('a') - 1;

$dir = "/Users/tonyw/test/";

opendir (DIR, $dir) || die "can't opendir $dir: $!";

while($old_name = readdir(DIR)) {

	$first = substr($old_name,0,1);
	next if $first eq '.';
	($name, $num) = split(' ', $old_name);
	$alpha = chr($start + $num);
	system("mv \"$dir$old_name\" \"$dir$name $alpha\"");
}
Just alter to taste and run.

Tony
honestpuck is offline   Reply With Quote
Old 08-18-2004, 02:26 AM   #4
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,334
And the winner of this week's perl obfusication contest is....

pmccann!

Congratulations. For your efforts you will receive a hardcover copy of O'Reilly's "Perl 6 for Power Users" (Hindi edition) and a Popeil Pocket Fisherman. Your name will also be entered into a draw for a two-week coding readability improvement seminar in the sunny McMurdo Sound this January!
acme.mail.order is offline   Reply With Quote
Old 08-18-2004, 02:57 AM   #5
honestpuck
Major Leaguer
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 256
Talking

Quote:
Originally Posted by acme.mail.order
And the winner of this week's perl obfusication contest is....

pmccann!

Congratulations. For your efforts you will receive a hardcover copy of O'Reilly's "Perl 6 for Power Users" (Hindi edition) and a Popeil Pocket Fisherman. Your name will also be entered into a draw for a two-week coding readability improvement seminar in the sunny McMurdo Sound this January!

Come on, acme! That's hardly obsfuscated at all. It only looks obscure 'cause it's on one line and he calculates the 26 squared plus 26 instead of just putting in 702 as a magic number. The rest is easy, a much nicer solution than mine.

Try this
Code:
@hash{1..702}=a..zz;
foreach $f(@ARGV){
    next unless (-f $f and ($g=$f)=~s/(\d+)$/$hash{$1}/);
    next if (-f $g); 
    rename($f,$g)
}
See, not obscure at all. Well, not for Perl.

Tony
honestpuck is offline   Reply With Quote
Old 08-18-2004, 03:06 AM   #6
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,334
I only said you won this week's contest, not grand champion! Loooooong way to go for that one.
acme.mail.order is offline   Reply With Quote
Old 08-18-2004, 08:08 AM   #7
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
OK, I promise to try harder next time. 'twas a long and busy day, and the five minutes to write that was probably the highlight. 'nuff said!

[[The 'one-liner' description is something of a running joke. Probably been running far too long... getting vewwwwwy tired...]]

Paul
pmccann is offline   Reply With Quote
Old 08-18-2004, 12:02 PM   #8
breen
Major Leaguer
 
Join Date: Jan 2003
Location: Bay Area
Posts: 327
Quote:
Originally Posted by acme.mail.order
...sunny McMurdo Sound this January!

Well, at least you're sending him there in local summer!
breen is offline   Reply With Quote
Old 08-20-2004, 10:38 AM   #9
retromango
Prospect
 
Join Date: Aug 2004
Posts: 3
thanks

I appreciate the input--I'll try these out later today. I suspected this could be done with a quick script but I can't speak perl.
retromango is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 10:36 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.