Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl Need to find Full Path of Directory/File
    text
    copied!<p>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.</p> <p>My Script:</p> <pre><code># 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"; } } </code></pre> <p>Here is my output:</p> <pre><code>------------------------------------------------------------------------ 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 ----------------------------------------------------------------------- </code></pre> <p>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.</p> <p>I know it's this part:</p> <pre><code>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 )... </code></pre> <p>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...</p> <p>Help please?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload