如何使用perl提升文件夹级别?

eanckbw9  于 6个月前  发布在  Perl
关注(0)|答案(2)|浏览(103)

我想知道搜索子文件夹,如果我已经在目录中。
例如:.. 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);
    }


sub 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)
}


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

my @arg = ('clean');
my @folder = ('logs');


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

cwdobuhd

cwdobuhd1#

对于使用路径,我强烈推荐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";

字符串

vof42yt1

vof42yt12#

也许像下面的代码片段可以阐明解决问题的可能方法。

use strict;
use warnings;
use feature 'say';

my $dir = '.';

folder_dive($dir);

sub folder_dive {
    my $folder = shift;
    
    say "In folder: $folder";
    
    for ( glob("$folder/*") ) {
        say "directory  $_"  if -d;     # if we got a directory
        say "file      $_"   if -f;     # if we got a file
        say "\ta file"                  if -f;
        say "\ta link"                  if -l;
        say "\ta socket"                if -S;
        say "\ta pipe"                  if -p;
        say "\ta block device"          if -b;
        say "\ta char device"           if -c;
        say "\ta text"                  if -T;
        say "\ta binary"                if -B;
        say "\treadable"                if -r;
        say "\twritable"                if -w;
        say "\texecutable"              if -x;
        say "\towned by user"           if -o;
        say "\treadable by real user"   if -R;
        say "\twritable by real user"   if -W;
        say "\texecutable by real user" if -X;
        say "\towned by real user"      if -O;
        say "\tzero size"               if -z;
        say "\tnonzero size"            if -s;
        say "\tsetuid"                  if -u;
        say "\tsetguid"                 if -g;
        say "\tsticky bit set"          if -k;
        say "\tisatty is true"          if -t;

        folder_dive("$_") if -d;        # if we got a directory     
    }
}

字符串
文档Perl File Test Operators

相关问题