I am recursively walking through directories but when a directory is empty and the script moves on to the next file/directory in the original directory, the Full Path stays in that empty directory. I need it change to the one I am currently searching through even when I go back up some levels.
My Script:
# 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";
}
}
Here is my output:
------------------------------------------------------------------------
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, and adstatlog.tgz are all actually in the original directory /home/gackerma/Logs, but the script thinks they're in the empty subdirectory AAA.
I know it's this part:
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 )...
But I don't know what to do to make it change the new Full Path to the new (original) directory if nothing is found in a subdirectory...
Help please?