0

ディレクトリを再帰的にたどっていますが、ディレクトリが空で、スクリプトが元のディレクトリの次のファイル/ディレクトリに移動すると、フル パスはその空のディレクトリに残ります。いくつかのレベルに戻っても、現在検索しているものに変更する必要があります。

私のスクリプト:

# specify the directory where you want to start the search
my $directory = $ARGV[0];
my $directoryCount = 0;
my $directory = shift;
my $searchfile = shift;
my @directories;
my $tarOutput;

# Calling the Subroutine, which searches the File
readDirectory($directory);

sub readDirectory
{
    # Open and close the directory
    opendir(DIR, $directory) or die("ERROR: Couldn't open specified directory $!");
    my @files = grep { $_ !~ /^\.{1,2}$/ } readdir DIR;
    closedir DIR;

    print "------------------------------------------------------------------------    \n\n";

    foreach my $currentFile (@files)
    {
        print "Current File: ", $currentFile, "\n\n";

        #directory currently searching through
        print "Searching in $directory\n\n";

        my $fullPath = "$directory/$currentFile";
        print "FULL PATH: $fullPath\n\n";
        if ( -d $fullPath )
        {
                print "Found New Directory: ", $currentFile, "\n\n";
                push (@directories, $currentFile);
                $directoryCount++;
                print "Current number = $directoryCount\n\n";
                print "Directories: @directories \n\n";
                # The Subroutine is calling hisself with the new parameters
                $directory = $fullPath;
                readDirectory($directory);
        }

        elsif ( $currentFile =~ /\.tar.gz$/i || $currentFile =~ /\.tar$/i ||     $currentFile =~ /\.gz$/i)
        {
                print "File: ", $currentFile, "\n\n";
                my $tarOutput = `tar -tvzf $currentFile`;
                print $tarOutput, "\n";
        }

        print "-----------------------------------------------------------------------\n\n";
    }
}

ここに私の出力があります:

------------------------------------------------------------------------

Current File: AAA

Searching in /home/gackerma/Logs

FULL PATH: /home/gackerma/Logs/AAA

Found New Directory: AAA

Current number = 1

Directories: AAA

------------------------------------------------------------------------

-----------------------------------------------------------------------

Current File: Dir1

Searching in /home/gackerma/Logs/AAA

FULL PATH: /home/gackerma/Logs/AAA/Dir1

-----------------------------------------------------------------------

Current File: file

Searching in /home/gackerma/Logs/AAA

FULL PATH: /home/gackerma/Logs/AAA/file

-----------------------------------------------------------------------

Current File: adstatlog.299

Searching in /home/gackerma/Logs/AAA

FULL PATH: /home/gackerma/Logs/AAA/adstatlog.299

-----------------------------------------------------------------------

Current File: zzz

Searching in /home/gackerma/Logs/AAA

FULL PATH: /home/gackerma/Logs/AAA/zzz

-----------------------------------------------------------------------

Current File: adstatlog.tgz

Searching in /home/gackerma/Logs/AAA

FULL PATH: /home/gackerma/Logs/AAA/adstatlog.tgz

-----------------------------------------------------------------------

Dir1、ファイル、adstatlog.299、zzz、および adstatlog.tgz はすべて、実際には元のディレクトリ /home/gackerma/Logs にありますが、スクリプトはそれらが空のサブディレクトリ AAA にあると認識しています。

私はそれがこの部分であることを知っています:

print "Current File: ", $currentFile, "\n\n";

#directory currently searching through
print "Searching in $directory\n\n";

my $fullPath = "$directory/$currentFile";
print "FULL PATH: $fullPath\n\n";
if ( -d $fullPath )...

しかし、サブディレクトリに何も見つからない場合、新しいフルパスを新しい(元の)ディレクトリに変更するために何をすべきかわかりません...

助けてください?

4

2 に答える 2