Perl需要找到目录/文件的完整路径

Perl需要找到目录/文件的完整路径,perl,recursion,directory,Perl,Recursion,Directory,我递归地遍历目录,但是当一个目录为空并且脚本移动到原始目录中的下一个文件/目录时,完整路径将保留在该空目录中。我需要它改变到一个我目前正在搜索,即使当我回到一些水平 我的剧本: # specify the directory where you want to start the search my $directory = $ARGV[0]; my $directoryCount = 0; my $directory = shift; my $searchfile = shift; my @d

我递归地遍历目录,但是当一个目录为空并且脚本移动到原始目录中的下一个文件/目录时,完整路径将保留在该空目录中。我需要它改变到一个我目前正在搜索,即使当我回到一些水平

我的剧本:

# 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 )...
但是如果在子目录中找不到任何内容,我不知道如何使它将新的完整路径更改为新的(原始)目录


请帮助?

为什么要通过编写自己的递归目录子来重新发明轮子?相反,您应该使用
文件::查找
文件::查找::规则
模块,该模块处理所有混乱的递归细节,除非对赋值执行此操作。

您使用的是一个名为$directory的全局变量,您永远不会更改它

sub readDirectory
{
        ...
}
应该是

sub readDirectory
{
        my ($directory) = @_;
        ...
}

有关
Wheel::Reinvent
File::Find
的一些替代方案,请参见。注意:File::Find有很多与之相关的问题。它就是不能正常工作。