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

eanckbw9  于 2023-11-22  发布在  Perl
关注(0)|答案(2)|浏览(315)

我想知道搜索子文件夹,如果我已经在目录中。
例如:.. build/clean/test/subfolder <-
我已经写好了如何进入测试的脚本,但不知道如何进入子文件夹。
脚本看起来像这样:Directory Handle in Perl Not Working Properly

  1. sub response {
  2. foreach my $arg (@ARGV) {
  3. print "Investigating $arg directory below:-\n";
  4. opendir(my $DIR, $arg) or die "You've Passed Invalid Directory as Arguments\n";
  5. my $size = 0;
  6. while(my $fileName = readdir $DIR) {
  7. next if $fileName =~ /^\./;
  8. my $path = File::Spec->catfile( $arg, $fileName );
  9. if( -d $path) {
  10. say "Folder1($arg, $fileName)"
  11. }
  12. $size++;
  13. }
  14. closedir $DIR;
  15. if($size == 0) {
  16. print "The $arg is an empty directory\n";
  17. }
  18. }

字符串
这是子文件夹

  1. sub Folder1
  2. {
  3. my $prevPath = shift;
  4. my $receivedFolder = shift;
  5. my $realPath = "$prevPath/$receivedFolder";
  6. my $path = File::Spec->rel2abs($realPath);
  7. print "$path\n";
  8. print "$receivedFolder Folder Received\n";
  9. foreach my $folder (@folder)
  10. {
  11. opendir(DIR, $path) or die "You've Passed Invalid Directory as Arguments\n";
  12. while(my $file = readdir $DIR) {
  13. next if $file =~ /^\./;
  14. my $path1 = File::Spec->catfile( $folder, $file );
  15. if( -d $path1) {
  16. say "Folder2($folder2, $file)"
  17. }
  18. closedir(DIR);
  19. }


sub 2文件夹

  1. sub Folder2 {
  2. my $prevPath1 = shift;
  3. my $receivedFolder1 = shift;
  4. my $realPath1 = "$prevPath1/$receivedFolder1";
  5. my $path1 = File::Spec->rel2abs($realPath1);
  6. print "$path1\n";
  7. print "$receivedFolder1 Folder Received\n";
  8. opendir(DIR, $path1) or die "You've Passed Invalid Directory as Arguments\n";
  9. while(my $file = readdir DIR) {
  10. print "Current Folder has $file file\n";
  11. }
  12. closedir(DIR)
  13. }


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

  1. my @arg = ('clean');
  2. my @folder = ('logs');


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

cwdobuhd

cwdobuhd1#

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

  1. use Path::Tiny 'path';
  2. my $path = path "/home/tai/Documents/";
  3. for my $child ( $path->children ) {
  4. print "$child\n" if $child->is_file;
  5. }
  6. my $parent = $path->parent;
  7. print "PARENT: $parent\n";

字符串

vof42yt1

vof42yt12#

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

  1. use strict;
  2. use warnings;
  3. use feature 'say';
  4. my $dir = '.';
  5. folder_dive($dir);
  6. sub folder_dive {
  7. my $folder = shift;
  8. say "In folder: $folder";
  9. for ( glob("$folder/*") ) {
  10. say "directory $_" if -d; # if we got a directory
  11. say "file $_" if -f; # if we got a file
  12. say "\ta file" if -f;
  13. say "\ta link" if -l;
  14. say "\ta socket" if -S;
  15. say "\ta pipe" if -p;
  16. say "\ta block device" if -b;
  17. say "\ta char device" if -c;
  18. say "\ta text" if -T;
  19. say "\ta binary" if -B;
  20. say "\treadable" if -r;
  21. say "\twritable" if -w;
  22. say "\texecutable" if -x;
  23. say "\towned by user" if -o;
  24. say "\treadable by real user" if -R;
  25. say "\twritable by real user" if -W;
  26. say "\texecutable by real user" if -X;
  27. say "\towned by real user" if -O;
  28. say "\tzero size" if -z;
  29. say "\tnonzero size" if -s;
  30. say "\tsetuid" if -u;
  31. say "\tsetguid" if -g;
  32. say "\tsticky bit set" if -k;
  33. say "\tisatty is true" if -t;
  34. folder_dive("$_") if -d; # if we got a directory
  35. }
  36. }

字符串
文档Perl File Test Operators

展开查看全部

相关问题