用perl做文件名批量修改
时间:2007-12-15 来源:tonywam1036
在linux下用wine环境调用Boox Viewer看《linux下perl编程》,貌似里面大部分内容都是抄<Perl5 Unleashed>,却不说是翻译,只说是原著,有点...。前天发现在linux下不能看<Perl5 Unleashed>,要改文件名,写了<正则表达式子模式的困惑>博文。现发现即使改了index.htm文件中的链接文件名仍有问题,原来一些链接是分散在其他htm文件中,包括gif图,要改不止一个文件了。换一个思路,就是所有文件名的大写改小写就行了,现在不改原始的htm文件了,把这个目录下的所有文件名第一个字母改为小写也同样达到目的,并且更简洁。在网上淘了下,发现一个perl程序比较好用。还要好好学学这个程序。
#!/usr/bin/perl
#########################################################################################################
#用法如下:
#
#移动到你要修改文件名的目录下
#rename.pl -l 会把该目录下的所有文件名改为小写,例如 Abcd.xx 会改为 abcd.xx
#rename.pl -u 会把该目录下的所有文件名改为大写,例如 AbcD.xx 会改为 ABCD.xx
#rename.pl -c 会把该目录下的所有文件名改为瘦子母大写,例如 abcd.xx 会改为Abcd.xx
#rename.pl -p yourPrefix 会把该目录下的所有文件名加前缀 yourPrefix, 例如 rename.pl -pimage_ 会把 abcd.xx 改为 image_abcd.xx
#rename.pl -s yourSuffix 同 -p 不过是添加后缀。
use Getopt::Std;
sub usage {
return "rename \n options: -l -u -c\n\t-l lowcase name\n\t-u upcase name\n\t-c capital name\n";
}
getopts("lucs:p:");
opendir D, "." or die "can noopen:$!";
if (!($opt_c||$opt_l||$opt_u||(defined$opt_s)||(defined $opt_p))) {#(())
print usage();
return;
}
while (defined ($file = readdir D)) {
$new = $file;
($bn, $sf) = split /\./,$file;
if ($opt_c) {
$bn =~ tr[A-Z][a-z];
$bn = ucfirst $bn;
}elsif ($opt_l) {
$bn =~ tr [A-Z][a-z];
}elsif ($opt_u) {
$bn =~ tr [a-z][A-Z];
}elsif (defined $opt_s) {
$bn .= $opt_s;#;;
}elsif (defined $opt_p){
$bn = $opt_p.$bn;
}
if ($sf ne "") {
$new = "$bn.$sf";
}else {
$new = $bn;
}
rename($file,$new);
}
# 程序来自:且听风吟 博客
#!/usr/bin/perl
#########################################################################################################
#用法如下:
#
#移动到你要修改文件名的目录下
#rename.pl -l 会把该目录下的所有文件名改为小写,例如 Abcd.xx 会改为 abcd.xx
#rename.pl -u 会把该目录下的所有文件名改为大写,例如 AbcD.xx 会改为 ABCD.xx
#rename.pl -c 会把该目录下的所有文件名改为瘦子母大写,例如 abcd.xx 会改为Abcd.xx
#rename.pl -p yourPrefix 会把该目录下的所有文件名加前缀 yourPrefix, 例如 rename.pl -pimage_ 会把 abcd.xx 改为 image_abcd.xx
#rename.pl -s yourSuffix 同 -p 不过是添加后缀。
use Getopt::Std;
sub usage {
return "rename \n options: -l -u -c\n\t-l lowcase name\n\t-u upcase name\n\t-c capital name\n";
}
getopts("lucs:p:");
opendir D, "." or die "can noopen:$!";
if (!($opt_c||$opt_l||$opt_u||(defined$opt_s)||(defined $opt_p))) {#(())
print usage();
return;
}
while (defined ($file = readdir D)) {
$new = $file;
($bn, $sf) = split /\./,$file;
if ($opt_c) {
$bn =~ tr[A-Z][a-z];
$bn = ucfirst $bn;
}elsif ($opt_l) {
$bn =~ tr [A-Z][a-z];
}elsif ($opt_u) {
$bn =~ tr [a-z][A-Z];
}elsif (defined $opt_s) {
$bn .= $opt_s;#;;
}elsif (defined $opt_p){
$bn = $opt_p.$bn;
}
if ($sf ne "") {
$new = "$bn.$sf";
}else {
$new = $bn;
}
rename($file,$new);
}
# 程序来自:且听风吟 博客
相关阅读 更多 +