【发布时间】:2020-10-16 18:19:33
【问题描述】:
如果我已经在子目录中,我想知道搜索子文件夹。
例如:..build/clean/test/subfolder
我已经编写了如何进行测试的脚本,但不知道如何进入子文件夹。
脚本如下所示: Directory Handle in Perl Not Working Properly
sub response {
foreach my $arg (@ARGV) {
print "Investigating $arg directory below:-\n";
opendir(my $DIR, $arg) or die "You've Passed Invalid Directory as Arguments\n";
my $size = 0;
while(my $fileName = readdir $DIR) {
next if $fileName =~ /^\./;
my $path = File::Spec->catfile( $arg, $fileName );
if( -d $path) {
say "Folder1($arg, $fileName)"
}
$size++;
}
closedir $DIR;
if($size == 0) {
print "The $arg is an empty directory\n";
}
}
这是子文件夹
sub Folder1
{
my $prevPath = shift;
my $receivedFolder = shift;
my $realPath = "$prevPath/$receivedFolder";
my $path = File::Spec->rel2abs($realPath);
print "$path\n";
print "$receivedFolder Folder Received\n";
foreach my $folder (@folder)
{
opendir(DIR, $path) or die "You've Passed Invalid Directory as Arguments\n";
while(my $file = readdir $DIR) {
next if $file =~ /^\./;
my $path1 = File::Spec->catfile( $folder, $file );
if( -d $path1) {
say "Folder2($folder2, $file)"
}
closedir(DIR);
}
子 2 文件夹
sub Folder2 {
my $prevPath1 = shift;
my $receivedFolder1 = shift;
my $realPath1 = "$prevPath1/$receivedFolder1";
my $path1 = File::Spec->rel2abs($realPath1);
print "$path1\n";
print "$receivedFolder1 Folder Received\n";
opendir(DIR, $path1) or die "You've Passed Invalid Directory as Arguments\n";
while(my $file = readdir DIR) {
print "Current Folder has $file file\n";
}
closedir(DIR)
}
我想阅读 sub 2 文件夹中的文件。但是,我继续使用 sub 2 文件夹返回主目录。
my @arg = ('clean');
my @folder = ('logs');
但是,在 build 目录中还有一个 logs 文件夹。它将被重定向到那里而不是 ../clean/test/logs
【问题讨论】:
-
请说明您以前在哪里,以及您采取了哪些步骤。最好发布您的代码。
-
我不清楚你想要达到什么目的。请不要将某些外部资源引用为“类似这样的东西”。而是包含您的特定代码并突出显示您在此代码中遇到的特定问题。
-
恐怕你的问题不是很清楚。提出明确的问题并不容易,但这是一项非常值得花时间掌握的技能。想象一下,您对自己的问题一无所知。你怎么能解释它,这样你的意思就很明显了。可能是这样的 - “我在这个目录中(...),我想检查这个目录中的文件(...)。我想要的输出看起来像这样(...)。这是我当前的代码(...)。我认为问题出在我用评论标记的那一行。”
-
给大家带来的误会请见谅!现在我会尽量让我的问题更清晰一些。感谢您的评论。