设为首页 收藏本站
查看: 838|回复: 0

[经验分享] perl 遍历windows系统拷贝想要的东西

[复制链接]

尚未签到

发表于 2018-8-31 07:24:16 | 显示全部楼层 |阅读模式
  use File::Copy ;
  sub Digui_GetFile {
  my $path = shift @_;
  my $ArrayPoint = shift @_;
  my $subpath;
  my $handle;
  if (-d $path)
  {
  if (opendir($handle, $path))
  {
  while ($subpath = readdir($handle))
  {
  if (!($subpath =~ m/^\.$/) && !($subpath =~ m/^(\.\.)$/))
  {
  my $NewPath = $path."/$subpath";
  if (-d $NewPath)
  {
  #是目录继续递归调用
  Digui_GetFile($NewPath,$ArrayPoint);
  }
  else
  {
  #不是目录存入
  $filecount++;
  push @$ArrayPoint,$NewPath."\n";
  }
  }
  }
  closedir($handle);
  }
  }
  return $filecount;
  }
  #将内容写入文本
  sub WriteToFile
  {
  my $FilePath = shift;
  my @WriteContent = @_;
  if(-d $FilePath)
  {
  print "这是一个目录,不是文件\n";
  return 1;
  }
  #以新建的方式打开文件
  open(FileOut,">$FilePath");
  foreach my $value(@WriteContent)
  {
  #去掉开头和结尾的空格
  $value =~ s/^\s+//;
  $value =~ s/\s+$//;
  #注意这里没有逗号
  print FileOut $value;
  }
  close(FileOut);
  }
  #获得以指定符号结尾的结果
  sub GetEndWithResult
  {
  my $File = shift;
  my $EndWith = shift;
  foreach my $value(@$EndWith)
  {
  #过滤掉空白行
  if($value =~ m/\s+/)
  {
  next;
  }
  my $FileName = substr($File,rindex($File,"/")+1,length($File) - rindex($File,"/") - 1);
  #开头加上逗号
  unless($value =~ m/^\./)
  {
  $value =~ s/^/\./;
  }
  if($FileName =~ m/$value$/)
  {
  #print "End with is Rum\n";
  #print "valuse is $value\n";
  #print "FileName is $FileName\n";
  #my $test = ;
  return 1;
  }
  }
  return 0;
  }
  #获得模糊匹配的结果
  sub GetContentwithResult
  {
  my $File = shift;
  my $ContentWith = shift;
  foreach my $value(@$ContentWith)
  {
  #过滤掉空白行
  if($value =~ m/\s+/)
  {
  next;
  }
  #过滤掉前后的空白
  s/^\s+//;
  s/\s+$//;
  my $FileName = substr($File,rindex($File,"/")+1,length($File) - rindex($File,"/") - 1);
  #模糊匹配
  if($FileName =~ m/$value/)
  {
  # print "contentwith is Rum\n";
  #  print "valuse is $value\n";
  #  print "FileName is $FileName\n";
  #  my $test = ;
  return 1;
  }
  }
  return 0;
  }
  #精确匹配包含的结果
  sub GetEqualWithResult
  {
  my $File = shift;
  my $EqualWith = shift;
  foreach my $value(@$EqualWith)
  {
  #过滤掉空白行
  if($value =~ m/\s+/)
  {
  next;
  }
  #过滤掉前后的空白
  $value =~ s/^\s+//;
  $value =~ s/\s+$//;
  #以exe结尾的后面要加上.lnk
  if($value =~ m/\.exe$/)
  {
  $value = $value.".lnk";
  }
  my $FileName = substr($File,rindex($File,"/")+1,length($File) - rindex($File,"/") - 1);
  #注意!!!!!!!!!!(perl中两个字符串不等,一般都是没有去掉首尾的空格!!!)
  $FileName =~ s/^\s+//;
  $FileName =~ s/\s+$//;
  #精确匹配
  if($FileName eq $value)
  {
  # print "Equal with is Rum\n";
  # print "valuse is $value\n";
  # print "FileName is $FileName\n";
  #my $test = ;
  return 1;
  }
  }
  return 0;
  }
  #获得最终的拷贝结果
  sub GetCopyResult
  {
  my $Result = shift;
  my $Endwith = shift;
  my $Contentwith = shift;
  my $EqualWith = shift;
  my $CopyResult = shift;
  #遍历目标目录下的所有文件
  foreach my $File(@$Result)
  {
  if(0 != @$Endwith)
  {
  #是否以指定符号结尾
  if(1 == &GetEndWithResult($File,\@$Endwith))
  {
  push @$CopyResult,$File;
  next;
  }
  }
  if(0 != @$Contentwith)
  {
  #是否模糊匹配一定字符串
  if(1 == &GetContentwithResult($File,\@$Contentwith))
  {
  push @$CopyResult,$File;
  next;
  }
  }
  if(0 != @$EqualWith)
  {
  #是否精确匹配一定字符串
  if(1 == &GetEqualWithResult($File,\@$EqualWith))
  {
  push @$CopyResult,$File;
  next;
  }
  }
  }
  return 1;
  }
  #读取拷贝配置文件
  sub ReadFromConfig
  {
  my $FilePath = shift;
  my $HashValue = shift;
  unless( -e $FilePath)
  {
  die "the config file does not exit";
  }
  #打开配置文件
  open(FileOut,"$FilePath");
  while(my $value = )
  {
  #获取key值
  my $key = substr($value,0,index($value,":")+1);
  my $Keyvalue =  substr($value,length($key),length($value) - length($key));
  #注意这里!!!哈希传引用赋值时是两个$$,第一个$HashValue是哈希的地址
  $$HashValue{$key} = $Keyvalue;
  }
  close FileOut;
  return 1;
  }
  my $ConfigeFilePath = "c:\\MYPERLCOPY\\PerlCopyConfig.txt";
  my %ConfigContent;
  &ReadFromConfig($ConfigeFilePath,\%ConfigContent);
  unless(exists $ConfigContent{"Source:"})
  {
  die "the sorce file dose not exit\n";
  }
  #获得拷贝源路径
  my $SrcCopyFile = $ConfigContent{"Source:"};
  print "src is $SrcCopyFile\n";
  #文件路径替换(注意这里的模式绑定符号=~)
  $SrcCopyFile =~ s/\\/\\\\/ig;
  #去掉开头的空白(注意路径一定要去掉开头和结尾的空白)
  $SrcCopyFile =~ s/^\s+//;
  $SrcCopyFile =~ s/\s+$//;
  unless(exists $ConfigContent{"Dest:"})
  {
  die "the sorce file dose not exit\n";
  }
  #获得拷贝目标路径
  my $DestCopyFile = $ConfigContent{"Dest:"};
  $DestCopyFile =~ s/\\/\\\\/ig;
  $DestCopyFile =~ s/^\s+//;
  $DestCopyFile =~ s/\s+$//;
  my @Result;
  my $Count = Digui_GetFile($SrcCopyFile,\@Result);
  print "Count is $Count\n";
  if(0 == $Count)
  {
  die "there is no file to copy\n";
  }
  #print "result is @Result\n";
  #得到以结尾结束的结果集
  my @EndWith;
  if(exists $ConfigContent{"EndWith:"})
  {
  @EndWith = split/,/,$ConfigContent{"EndWith:"};
  }
  #获得模糊匹配的结果集
  my @ContentWith;
  if(exists $ConfigContent{"ContentWith:"})
  {
  @ContentWith = split/,/,$ConfigContent{"ContentWith:"};
  }
  #获得精确匹配的结果集
  my @EqualWith;
  if(exists $ConfigContent{"EqualWith:"})
  {
  @EqualWith = split/,/,$ConfigContent{"EqualWith:"};
  }
  #获得过滤的结果
  my @CopyResult;
  &GetCopyResult(\@Result,\@EndWith,\@ContentWith,\@EqualWith,\@CopyResult);
  #print "result is @CopyResult\n";
  #将文件逐个进行拷贝
  foreach my $value (@CopyResult)
  {
  $value =~ s/^\s+//;
  $value =~ s/\s+$//;
  print "value is $value\n";
  print "dest is $DestCopyFile";
  #my $test = ;
  #&File::Copy::copy($value, $DestCopyFile) or die "Can not copy file!!!";
  copy($value, $DestCopyFile) or die "Can not copy file!!!";
  }
  my $CopyOrMove = $ConfigContent{"COPY:"};
  #是否删除文件
  if($CopyOrMove =~ m/false/i)
  {
  foreach my $value (@CopyResult)
  {
  unlink($value);
  }
  }


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-559063-1-1.html 上篇帖子: [Perl] File::Basename用法 下篇帖子: perl发送邮件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表