Я рекурсивно прохожу по каталогам, но когда каталог пуст и сценарий переходит к следующему файлу/директории в исходном каталоге, полный путь остается в этом пустом каталоге. Мне нужно, чтобы он изменился на тот, который я просматриваю, даже когда я возвращаюсь на несколько уровней.Perl Нужно найти полный путь к каталогу/файлу
Мой сценарий:
# 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 все на самом деле в исходном каталоге/дома/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)...
Но я не знаю, что сделать, чтобы изменить новый полный путь к новой (исходной) директории, если ничего не найдено в подкаталоге .. .
Помогите, пожалуйста?
Также см http://stackoverflow.com/a/1692697/55857 для некоторых альтернатив как 'Wheel :: Reinvent' и' File :: find'. – FMc