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、files、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

1

为什么要通过编写自己的递归目录子来重新发明轮子?相反,您应该使用File::FindorFile::Find::Rule模块,它处理所有混乱的递归细节,除非这是为分配完成的。

于 2013-05-10T14:57:56.293 回答
0

您正在使用一个名为 $directory 的全局变量,该变量永远不会更改。

sub readDirectory
{
        ...
}

应该

sub readDirectory
{
        my ($directory) = @_;
        ...
}
于 2013-05-10T14:58:26.143 回答