开发者

Perl Move Files

I'm using this code for moving files from directories but I need 2 changes to make and would appreciate some help.

I want "return unless" the found directory /^_temp\z/s contains cue && toc && accurip && flac files. If one of these extensions is missing go to next directory.

And if this condition is true - move files from DIR, '.' directory to $dir_dump, but with copied parent folder and if the first parent folder contains "Disc \d" to copy the second parent folder and move $dir_dump\$parent_开发者_运维知识库folder\$disc.

find(\&temp, $dir_target); 
sub temp {
my $dir_dump = "E:/_dump/";
my ($dev,$ino,$mode,$nlink,$uid,$gid);
return unless ((($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -d _ && /^\_temp\z/s);
    chdir($File::Find::dir) or die "could not cd to $File::Find::dir: $!";
    # mv files from cur dir to $dir_dump
    opendir DIR, '.' or die "could not open dir: $!";
    my @files = grep m{\.(?:accurip|cue|flac|log|toc|wv)\z}, readdir DIR;
    closedir DIR;
foreach my $file (@files) {
    mv $file, $dir_dump or die "could not mv $file to $dir_dump: $!";
}
# mv files from temp to cur dir
    opendir DIR, '_temp' or die "could not open temp dir: $!";
@files = grep m{\.(?:accurip|cue|flac|toc)\z}, readdir DIR;
    closedir DIR;
foreach my $file (@files) {
    mv "_temp/$file", '.' or die "could not mv $file from _temp: $!";
}
finddepth(sub{rmdir},'.');
}


To check if the directory contains files with all 4 extensions, you can do this:

my @dir_files = readdir DIR; # after you did opendir
my %extensions = map { (/\.([^.]+)$/)=> 1 } @dir_files;
foreach my $required_ext (qw(accurip cue flac toc)) {
    return unless $extensions{$required_ext};
}

I must admit I can't quite understand your second requirement, sorry. May be you could provide examples?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜