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

perl 遍历windows系统拷贝想要的东西

发布时间:2020-12-16 00:11:02 所属栏目:大数据 来源:网络整理
导读: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))? ?

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?Rumn";
??????#print?"valuse?is?$valuen";
??????#print?"FileName?is?$FileNamen";?
??????#my?$test?=?<stdin>;
???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,"/")?-?1);
?????#模糊匹配
??if($FileName?=~?m/$value/)
??{

?????#?print?"contentwith?is?Rumn";
????#??print?"valuse?is?$valuen";
????#??print?"FileName?is?$FileNamen";?
????#??my?$test?=?<stdin>;
???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,"/")?-?1);
????????#注意!!!!!!!!!!(perl中两个字符串不等,一般都是没有去掉首尾的空格!!!)
??$FileName?=~?s/^s+//;
????????$FileName?=~?s/s+$//;

??#精确匹配
??if($FileName?eq?$value)
??{??????
?????#?print?"Equal?with?is?Rumn";
?????#?print?"valuse?is?$valuen";
?????#?print?"FileName?is?$FileNamen";?
??????#my?$test?=?<stdin>;

???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?=?<FileOut>)
?{
?????#获取key值
??my?$key?=?substr($value,index($value,":")+1);
??my?$Keyvalue?=??substr($value,length($key),length($value)?-?length($key));
??#注意这里!!!哈希传引用赋值时是两个$$,第一个$HashValue是哈希的地址
??$$HashValue{$key}?=?$Keyvalue;??
?}

?close?FileOut;
?return?1;
}


my?$ConfigeFilePath?=?"c:MYPERLCOPYPerlCopyConfig.txt";
my?%ConfigContent;
&ReadFromConfig($ConfigeFilePath,%ConfigContent);

unless(exists?$ConfigContent{"Source:"})
{
?die?"the?sorce?file?dose?not?exitn";
}
#获得拷贝源路径
my?$SrcCopyFile?=?$ConfigContent{"Source:"};
print?"src?is?$SrcCopyFilen";
#文件路径替换(注意这里的模式绑定符号=~)
$SrcCopyFile?=~?s//\/ig;
#去掉开头的空白(注意路径一定要去掉开头和结尾的空白)
$SrcCopyFile?=~?s/^s+//;
$SrcCopyFile?=~?s/s+$//;
unless(exists?$ConfigContent{"Dest:"})
{
?die?"the?sorce?file?dose?not?exitn";
}
#获得拷贝目标路径
my?$DestCopyFile?=?$ConfigContent{"Dest:"};
$DestCopyFile?=~?s//\/ig;
$DestCopyFile?=~?s/^s+//;
$DestCopyFile?=~?s/s+$//;
my?@Result;
my?$Count?=?Digui_GetFile($SrcCopyFile,@Result);
print?"Count?is?$Countn";
if(0?==?$Count)
{
?die?"there?is?no?file?to?copyn";
}

#print?"result?is?@Resultn";

#得到以结尾结束的结果集
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?@CopyResultn";

#将文件逐个进行拷贝
foreach?my?$value?(@CopyResult)
{
????$value?=~?s/^s+//;
????$value?=~?s/s+$//;?
????print?"value?is?$valuen";
?print?"dest?is?$DestCopyFile";
?#my?$test?=?<stdin>;
?#&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);?}}

(编辑:李大同)

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

    推荐文章
      热点阅读