好久没有写过代码,昨天鼓捣了一下,根据自己的需要写了一段Perl代码,(汗,接拼凑的),有些地方自己现在理解起来都有些吃力了!代码还是很久前的。
#!/usr/bin/perl
# rename.pl by IAwen <>
# Copyright (c) IAwen
# 对指定的文件夹里的音乐文件(mp3或者wma)进行乱序重命名
[codes=python]
use strict;
use warnings;
main(@ARGV);
sub main
{
my $localDir = shift or die “请提供文件路径!\n格式如:rename.tpl F:\\Temp\\demo\\\n\n”;
opendir(TEMP, $localDir) || die “无法打开路径: $localDir”;
my @dirlist = grep(/(\.mp3|\.wma)$/i,readdir TEMP);
closedir TEMP;
fisher_yates_shuffle( \@dirlist );
my $n = “001”;
foreach my $fn (@dirlist){
my $file_t = “$localDir$fn”;
my ($ext) = ($fn =~ /(\.mp3|\.wma)$/i);
my ($fname) = ($fn =~ /([^\.]+)\.*/);
rename($file_t, “$localDir$n-$fname$ext “);
message(“文件:$fn \n—>重命名为:$n-$fname$ext\n”);
$n++;
}
}
sub message{
my $m = shift or return;
print(“$m\n”)
}
# 数组乱序
sub fisher_yates_shuffle {
my $deck = shift; # $deck is a reference to an array
my $i = @$deck;
while (–$i) {
my $j = int rand ($i+1);
@$deck[$i,$j] = @$deck[$j,$i];
}
}