自学内容网 自学内容网

perl批量改文件后缀

perl批量改文件后缀

如题,perl批量改文件后缀,将已有的统一格式的文件后缀,修改为新的统一的文件后缀。

#!/bin/perl
use 5.010;

print "Please input file suffix which U want to rename!\n";
chomp (my $suffix_old = <>); # 去除输入中的回车符
print "Please output file suffix which U want to rename!\n";
chomp (my $suffix_new = <>);

foreach my $file(glob "*.$suffix_old") {
  my $new_file = $file;
  $new_file =~ s/\.$suffix_old/.$suffix_new/gm;
  if (-e $new_file) {
    warn "Cannot rename $file to $new_file: $new_file exists\n";
  } elsif (rename $file => $new_file) {
  } else {
    warn "rename $file to $new_file failed:$!";
  }
}

原文地址:https://blog.csdn.net/weixin_38368252/article/details/143018631

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!