|
|
#1 |
|
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? |
|
|
|
|
|
#2 |
|
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 |
|
|
|
|
|
#3 | |||||||||||||||||||||||
|
Major Leaguer
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 256
|
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\"");
}
Tony |
|||||||||||||||||||||||
|
|
|
|
|
#4 |
|
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! |
|
|
|
|
|
#5 | |||||||||||||||||||||||
|
Major Leaguer
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 256
|
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)
}
Tony |
|||||||||||||||||||||||
|
|
|
|
|
#6 |
|
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.
|
|
|
|
|
|
#7 |
|
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 |
|
|
|
|
|
#8 | |||||||||||||||||||||||
|
Major Leaguer
Join Date: Jan 2003
Location: Bay Area
Posts: 327
|
Well, at least you're sending him there in local summer! |
|||||||||||||||||||||||
|
|
|
|
|
#9 |
|
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.
|
|
|
|
![]() |
|
|