如何使用 perl 进入上一级文件夹?

Aly*_*Low 2 directory perl

如果我已经在子目录中,我想知道如何搜索子文件夹。

例如:..build/clean/test/子文件夹 <-----

我已经编写了如何进行测试的脚本,但不知道如何进入子文件夹。

该脚本看起来像这样: Directory Handle in Perl Not Operating 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";
    }
}
Run Code Online (Sandbox Code Playgroud)

这是子文件夹

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);
    }
Run Code Online (Sandbox Code Playgroud)

子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)
}
Run Code Online (Sandbox Code Playgroud)

我想读取 sub 2 文件夹内的文件。但是,我一直使用 sub 2 文件夹返回主目录。

my @arg = ('clean');
my @folder = ('logs');
Run Code Online (Sandbox Code Playgroud)

但是,在构建目录中还有一个日志文件夹。它将被重定向到那里而不是 ../clean/test/logs

tob*_*ink 5

对于使用路径,我强烈推荐Path::Tiny

use Path::Tiny 'path';

my $path = path "/home/tai/Documents/";

for my $child ( $path->children ) {
   print "$child\n" if $child->is_file;
}

my $parent = $path->parent;
print "PARENT: $parent\n";
Run Code Online (Sandbox Code Playgroud)