search - Perl Directory Walking issue - Can't go back up more than 1 directory properly -


first off, don't have ability use file::find.

so have script walk through directories , find type of file. if go more 1 sub-directory deep, script doesn't exit way starting directory. think need have $previousdir variable keeps track of last directory can go out 1 when i'm done in sub-directory. i've tried putting in multiple places without success...

file structure (bold dir, italic file):

startingdirectory/logs - aaa, dir1, zzz, adstatlog.299, adstatlog.tgz, file

/aaa - filefile

/dir1 - /dir2, config.tar.gz

/dir2 - empty

/zzz - withinzzz

here current script:

# specify directory want start search $startingdir = $argv[0]; $directorycount = 0; $directory = shift; $previousdir; @directories; $taroutput;  # calling subroutine, searches directory readdirectory($startingdir);  sub readdirectory {     # open , close startingdir     opendir(dir, @_[0]) or die("error: couldn't open specified directory $!");     @files = grep { $_ !~ /^\.{1,2}$/ } readdir dir;     closedir dir;      print "------------------------------------------------------------------------\n\n";      foreach $currentfile (@files)     {            print "current file: ", $currentfile, "\n\n";          #directory searching through         print "searching in $directory\n\n";          $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;             # subroutine calling hisself new parameters             readdirectory($directory);         }          elsif ( $currentfile =~ /\.tar.gz$/i || $currentfile =~ /\.tar$/i || $currentfile =~ /\.tgz$/i)         {             print "file: ", $currentfile, "\n\n";             $taroutput = `tar -tvzf $currentfile`;             print $taroutput, "\n";             $previousdir = $directory;         }          print "previousdir: $previousdir\n\n";          print "-----------------------------------------------------------------------\n\n";          $directory = $previousdir;     }    } 

and output: (scroll down see issue begins)

------------------------------------------------------------------------  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 issue starts -                                            previousdir should /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  ------------------------------------------------------------------ 

i use file::find if can.

here's working, simplified version of recursive try:

use warnings; use strict;  die "usage: $0 (abs path dir) " if @argv != 1;  $dir = shift @argv;  file_find($dir);  sub file_find {     $dir = shift;      opendir $dh, $dir or warn "$dir: $!";     @files = grep { $_ !~ /^\.{1,2}$/ } readdir $dh;     closedir $dh;      $file ( @files ) {          $path = "$dir/$file";          if( $path =~ /(\.tar\.gz|\.tar|\.tgz)$/ ) {              print "do tar $path\n";         }            file_find($path) if -d $path;     }    } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -