如何递归复制目录并在 Perl 中过滤文件名? [英] How can I copy a directory recursively and filter filenames in Perl?

查看:23
本文介绍了如何递归复制目录并在 Perl 中过滤文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何复制包含子目录的目录,不包括与 Windows 系统上的某个正则表达式匹配的文件或目录?

How do I copy a directory including sub directories excluding files or directories that match a certain regex on a Windows system?

推荐答案

我会这样做:

use File::Copy;
sub copy_recursively {
    my ($from_dir, $to_dir, $regex) = @_;
    opendir my($dh), $from_dir or die "Could not open dir '$from_dir': $!";
    for my $entry (readdir $dh) {
        next if $entry =~ /$regex/;
        my $source = "$from_dir/$entry";
        my $destination = "$to_dir/$entry";
        if (-d $source) {
            mkdir $destination or die "mkdir '$destination' failed: $!" if not -e $destination;
            copy_recursively($source, $destination, $regex);
        } else {
            copy($source, $destination) or die "copy failed: $!";
        }
    }
    closedir $dh;
    return;
}

这篇关于如何递归复制目录并在 Perl 中过滤文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆