recursion - Perl Need to find Full Path of Directory/File -


i recursively walking through directories when directory empty , script moves on next file/directory in original directory, full path stays in empty directory. need change 1 searching through when go levels.

my script:

# specify directory want start search $directory = $argv[0]; $directorycount = 0; $directory = shift; $searchfile = shift; @directories; $taroutput;  # calling subroutine, searches file readdirectory($directory);  sub readdirectory {     # open , close directory     opendir(dir, $directory) 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";                 # subroutine calling hisself new parameters                 $directory = $fullpath;                 readdirectory($directory);         }          elsif ( $currentfile =~ /\.tar.gz$/i || $currentfile =~ /\.tar$/i ||     $currentfile =~ /\.gz$/i)         {                 print "file: ", $currentfile, "\n\n";                 $taroutput = `tar -tvzf $currentfile`;                 print $taroutput, "\n";         }          print "-----------------------------------------------------------------------\n\n";     } } 

here 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, , adstatlog.tgz in original directory /home/gackerma/logs, script thinks they're in empty subdirectory aaa.

i know it's part:

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 )... 

but don't know make change new full path new (original) directory if nothing found in subdirectory...

help please?

why reinvent wheel writing own recursive directory sub? instead, should using either file::find or file::find::rule module, handles of messy recursion details unless being done assignment.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -