Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl Directory Walking issue - Can't go back up more than 1 directory properly
    primarykey
    data
    text
    <p>First off, I don't have the ability to use File::Find.</p> <p>So I have my script to walk through directories and find a certain type of file. But if I go more than one sub-directory deep, my script doesn't properly exit all the way back up to the starting directory. I think I need to have a $previousDir variable that keeps track of the last directory so I can say to go back out to that one when I'm done in the sub-directory. But I've tried putting it in multiple places without success...</p> <p>File Structure (<strong>BOLD</strong> is a Dir, <em>Italic</em> is a file): </p> <p>startingdirectory/Logs - <strong>AAA</strong>, <strong>Dir1</strong>, <strong>zzz</strong>, <em>adstatlog.299, adstatlog.tgz, file</em></p> <p>/<strong>AAA</strong> - <em>filefile</em></p> <p>/<strong>Dir1</strong> - /<strong>Dir2</strong>, <em>config.tar.gz</em></p> <p>/<strong>Dir2</strong> - EMPTY</p> <p>/<strong>zzz</strong> - <em>withinzzz</em></p> <p>Here is my current script:</p> <pre><code># specify the directory where you want to start the search my $startingDir = $ARGV[0]; my $directoryCount = 0; my $directory = shift; my $previousDir; my @directories; my $tarOutput; # Calling the Subroutine, which searches the Directory readDirectory($startingDir); sub readDirectory { # Open and close the startingDir opendir(DIR, @_[0]) 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"; $previousDir = $directory; $directory = $fullPath; # The Subroutine is calling hisself with the new parameters readDirectory($directory); } elsif ( $currentFile =~ /\.tar.gz$/i || $currentFile =~ /\.tar$/i || $currentFile =~ /\.tgz$/i) { print "File: ", $currentFile, "\n\n"; my $tarOutput = `tar -tvzf $currentFile`; print $tarOutput, "\n"; $previousDir = $directory; } print "PREVIOUSDIR: $previousDir\n\n"; print "-----------------------------------------------------------------------\n\n"; $directory = $previousDir; } } </code></pre> <p>And the output: (scroll down to see where issue begins)</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: filefile Searching in /home/gackerma/Logs/AAA FULL PATH: /home/gackerma/Logs/AAA/filefile PREVIOUSDIR: /home/gackerma/Logs ------------------------------------------------------------------ PREVIOUSDIR: /home/gackerma/Logs ------------------------------------------------------------------ Current File: Dir1 Searching in /home/gackerma/Logs FULL PATH: /home/gackerma/Logs/Dir1 Found New Directory: Dir1 Current number = 2 Directories: AAA Dir1 ------------------------------------------------------------------------ Current File: DIR2 Searching in /home/gackerma/Logs/Dir1 FULL PATH: /home/gackerma/Logs/Dir1/DIR2 Found New Directory: DIR2 Current number = 3 Directories: AAA Dir1 DIR2 ------------------------------------------------------------------------ PREVIOUSDIR: /home/gackerma/Logs/Dir1 ------------------------------------------------------------------ Current File: configs.tar.gz Searching in /home/gackerma/Logs/Dir1 FULL PATH: /home/gackerma/Logs/Dir1/configs.tar.gz PREVIOUSDIR: /home/gackerma/Logs/Dir1 ------------------------------------------------------------------ PREVIOUSDIR: /home/gackerma/Logs/Dir1 ***THIS IS WHERE THE ISSUE STARTS - PREVIOUSDIR SHOULD BE /Logs!!*** ------------------------------------------------------------------ Current File: file Searching in /home/gackerma/Logs/Dir1 FULL PATH: /home/gackerma/Logs/Dir1/file PREVIOUSDIR: /home/gackerma/Logs/Dir1 ------------------------------------------------------------------ Current File: adstatlog.299 Searching in /home/gackerma/Logs/Dir1 FULL PATH: /home/gackerma/Logs/Dir1/adstatlog.299 PREVIOUSDIR: /home/gackerma/Logs/Dir1 ------------------------------------------------------------------ Current File: zzz Searching in /home/gackerma/Logs/Dir1 FULL PATH: /home/gackerma/Logs/Dir1/zzz PREVIOUSDIR: /home/gackerma/Logs/Dir1 ------------------------------------------------------------------ Current File: adstatlog.tgz Searching in /home/gackerma/Logs/Dir1 FULL PATH: /home/gackerma/Logs/Dir1/adstatlog.tgz PREVIOUSDIR: /home/gackerma/Logs/Dir1 ------------------------------------------------------------------ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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