您的位置:首页 > 其它

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

2013-07-14 14:18 429 查看
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 = <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,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 = <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,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 = <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,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 = <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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  windows perl 递归拷贝