在perl中查找没有其他子文件夹的文件夹
如何在给定路径中找到没有其他子文件夹的所有文件夹?它们可能包含文件,但不包含其他文件夹 例如,给定以下目录结构:在perl中查找没有其他子文件夹的文件夹,perl,find,Perl,Find,如何在给定路径中找到没有其他子文件夹的所有文件夹?它们可能包含文件,但不包含其他文件夹 例如,给定以下目录结构: time/aa/ time/aa/bb time/aa/bb/something/* time/aa/bc time/aa/bc/anything/* time/aa/bc/everything/* time/ab/ time/ab/cc time/ab/cc/here/* time/ab/cc/there/* time/ab/cd time/ab/cd/everywhere/* t
time/aa/
time/aa/bb
time/aa/bb/something/*
time/aa/bc
time/aa/bc/anything/*
time/aa/bc/everything/*
time/ab/
time/ab/cc
time/ab/cc/here/*
time/ab/cc/there/*
time/ab/cd
time/ab/cd/everywhere/*
time/ac/
find(time)
的输出应如下所示:
time/aa/bb/something/*
time/aa/bc/anything/*
time/aa/bc/everything/*
time/ab/cc/here/*
time/ab/cc/there/*
time/ab/cd/everywhere/*
上面的
*
表示文件。此函数将完成此操作。只需使用初始路径调用它:
sub isChild {
my $folder = shift;
my $isChild = 1;
opendir(my $dh, $folder) || die "can't opendir $folder: $!";
while (readdir($dh)) {
next if (/^\.{1,2}$/); # skip . and ..
if (-d "$folder/$_") {
$isChild = 0;
isChild("$folder/$_");
}
}
closedir $dh;
if ($isChild) { print "$folder\n"; }
}
基本上,打开根文件夹并使用以下过程:
sub child_dirs {
my ($directory) = @_;
opendir my $dir, $directory or die $!;
my @subdirs = grep {-d $_ and not m</\.\.?$>} map "$directory/$_", readdir $dir;
# ^-- directory and not . or .. ^-- use full name
$ leaf-dirs time
time/aa/bb/something
time/aa/bc/anything
time/aa/bc/everything
time/ab/cc/here
time/ab/cc/there
time/ab/cd/everywhere
opendir my $dir, $directory or die $!;
my @subdirs = grep {-d $_ and not m</\.\.?$>} map "$directory/$_", readdir $dir;
# ^-- directory and not . or .. ^-- use full name
$ leaf-dirs time
time/aa/bb/something
time/aa/bc/anything
time/aa/bc/everything
time/ab/cc/here
time/ab/cc/there
time/ab/cd/everywhere
say $_ for child_dirs("time"); # dir `time' has to be in current directory.
每当您想编写目录遍历程序时,总是使用标准模块。在处理文件系统时,您必须能够处理奇怪的情况,而天真的实现很少这样做
(文档中名为want
的)有三个变量,它们对您想要做的事情特别有用
$File::Find::dir
是当前目录名
$是该目录中的当前文件名
opendir my $dir, $directory or die $!;
my @subdirs = grep {-d $_ and not m</\.\.?$>} map "$directory/$_", readdir $dir;
# ^-- directory and not . or .. ^-- use full name
$ leaf-dirs time
time/aa/bb/something
time/aa/bc/anything
time/aa/bc/everything
time/ab/cc/here
time/ab/cc/there
time/ab/cd/everywhere
$File::Find::name
是文件的完整路径名
当我们发现一个目录不是
或。
时,我们会记录完整的路径并删除它的父目录,现在我们知道它不能是叶目录。最后,任何保留的记录路径都必须保留,因为执行了一个
您可以对当前目录的子目录运行它
opendir my $dir, $directory or die $!;
my @subdirs = grep {-d $_ and not m</\.\.?$>} map "$directory/$_", readdir $dir;
# ^-- directory and not . or .. ^-- use full name
$ leaf-dirs time
time/aa/bb/something
time/aa/bc/anything
time/aa/bc/everything
time/ab/cc/here
time/ab/cc/there
time/ab/cd/everywhere
$leaf dirs时间
时间/aa/bb/什么的
时间/aa/bc/任何事情
时间/aa/bc/一切
时间/ab/cc/这里
时间/ab/cc/那里
时间/ab/cd/无处不在
或者使用完整路径
$ leaf-dirs /tmp/time
/tmp/time/aa/bb/something
/tmp/time/aa/bc/anything
/tmp/time/aa/bc/everything
/tmp/time/ab/cc/here
/tmp/time/ab/cc/there
/tmp/time/ab/cd/everywhere
$leaf dirs/tmp/次
/tmp/时间/aa/bb/什么的
/tmp/time/aa/bc/anything
/tmp/时间/aa/bc/一切
/tmp/time/ab/cc/here
/tmp/time/ab/cc/there
/tmp/time/ab/cd/everywhere
或在同一调用中垂直排列多个目录
$ mkdir -p /tmp/foo/bar/baz/quux
$ leaf-dirs /tmp/time /tmp/foo
/tmp/foo/bar/baz/quux
/tmp/time/aa/bb/something
/tmp/time/aa/bc/anything
/tmp/time/aa/bc/everything
/tmp/time/ab/cc/here
/tmp/time/ab/cc/there
/tmp/time/ab/cd/everywhere
$mkdir-p/tmp/foo/bar/baz/qux
$leaf dirs/tmp/time/tmp/foo
/tmp/foo/bar/baz/quux
/tmp/时间/aa/bb/什么的
/tmp/time/aa/bc/anything
/tmp/时间/aa/bc/一切
/tmp/time/ab/cc/here
/tmp/time/ab/cc/there
/tmp/time/ab/cd/everywhere我尝试了readdir的做事方式。然后我偶然发现了这个
use File::Find::Rule;
# find all the subdirectories of a given directory
my @subdirs = File::Find::Rule->directory->in( $directory );
我从此输出中删除了任何与字符串的初始部分匹配的条目,并且没有一些叶条目。到目前为止您尝试了什么,哪里遇到了问题,您对已经编写的代码有什么疑问?我们不是一个“为我编写解决方案”的网站。如果您不知道从哪里开始,可以查看File::Find:grep的良好使用缩短了时间!