`
zengshaotao
  • 浏览: 752420 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

perl 把源目录下的文件拷贝到目标目录(带路径)

    博客分类:
  • perl
 
阅读更多

原文地址:http://blog.csdn.net/qin123qq/article/details/41083741

 

#!/bin/perl
#perl "D:\separateFliesBySearch.pl" "D:/WebRoot" "C:/Users/Documents/" ".xml" "<beans xmlns=\"http:\/\/www\.springframework\.org\/schema\/beans\""
use strict;
use warnings;
use File::Find;
use File::Copy;
use File::Path;

die "使用的参数:$0,不符合要求,请指定目录及搜索内容" unless @ARGV>=4;
my $srcdir = $ARGV[0];
my $distdir = $ARGV[1];
my $suffix = $ARGV[2];
my $reg = $ARGV[3];
my $verbose = 0;
if($ARGV[4])
{
 $verbose = $ARGV[4];
}

searchContentsInFiles($srcdir,$suffix,$reg);

sub searchContentsInFiles
{
 my $srcdir=$_[0];
 my $suffix=$_[1];
 
 if(! -e $distdir)
 {
  #第二个参数为是否显示日志,第三个目录权限
  mkpath($distdir,$verbose,0711);
 }
 my @filenames=find( \&wanted,$srcdir);
 
 close FH;
}

sub wanted
{
 my $file = $File::Find::name;
 my $filename=$_;
 my $filedir=$File::Find::dir;
 if(-f $file && /$suffix/ && !/\.svn/)
 {
  open(FH,"$file");
  if(grep {/$reg/} <FH>)
  {
   #$_="$filedir";
   my $tmpfiledir=$filedir;
   $tmpfiledir=~ s/$srcdir//;
   my $tmpdistdir="$distdir$tmpfiledir";
   #print "$tmpdistdir\n";
   if(!-e "$tmpdistdir")
   {
    #第二个参数为是否显示日志,第三个目录权限
    mkpath("$tmpdistdir",$verbose,0711);
   }
   copy($file,"$tmpdistdir");
  }
 }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics