我递归遍历目录但当目录为空并且脚本移动到原始目录中的下一个文件/目录时,完整路径将保留在该空目录中。我需要改变我正在搜索的那个,即使我回到某个级别。
我的剧本:
# 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 )...
但我不知道如果在子目录中找不到任何内容,它会将新的Full Path更改为新的(原始)目录...
请帮忙吗?
答案 0 :(得分:1)
为什么要通过编写自己的递归目录子来重新发明轮子?相反,您应该使用File::Find
或File::Find::Rule
模块,该模块处理所有混乱的递归细节,除非为分配执行此操作。
答案 1 :(得分:0)
你正在使用一个名为$ directory的全局变量,你永远不会改变它。
sub readDirectory
{
...
}
应该是
sub readDirectory
{
my ($directory) = @_;
...
}