加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

批量修改目录及其子目录的文件名(perl脚本)

发布时间:2020-12-15 21:02:18 所属栏目:大数据 来源:网络整理
导读:功能:把当前目录及其子目录里的文件名批量修改 #!/usr/perl#批量修改文件名find_fileindir(".");#在当前目录执行sub find_fileindir(){ local($dir) = @_; opendir(DIR,"$dir"|| die "can't open this $dir"); local @files =readdir(DIR); closedir(DIR);

功能:把当前目录及其子目录里的文件名批量修改

#!/usr/perl
#批量修改文件名

&find_fileindir(".");#在当前目录执行
sub find_fileindir(){
  local($dir) = @_;
  opendir(DIR,"$dir"|| die "can't open this $dir");
  local @files =readdir(DIR);
  closedir(DIR);
  for $file (@files){
    next if($file=~m/.$/ || $file =~m/..$/);#剔除.和..
    if ($file =~/.(cpp|cc|c)$/i){#文件扩展名为cpp或者cc或者c的文件
        print "$dir/$file n";
        my $newFile = $file;
	$newFile =~ s/cpp/h/; #将“cpp”扩展名改为“h”
	if(-e $newFile){ #如果修改后会导致文件重名,则输出警告,不作处理
	warn "Can't rename $file to $newFile. The $newFile exists!n";
	}else{
	rename "$dir/$file","$dir/$newFile" #重命名文件
	or
	warn "Rename $file to $newFile failed: $!n"; #如果重命名失败,则输出警告
????}
    elsif(-d "$dir/$file"){
        find_fileindir("$dir/$file" );
    }
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读