Maverick
Maverick

Reputation: 587

Not Able to get the full path of the file

I have written a script which will do :

  1. Search a file recursively
  2. Get the unique(desired one) file
  3. Crate a directory on remotely placed server
  4. copy this files in respective directory

OS: Windows7

I got stuck at 4 steps (copy this files in respective directory),because i am unable to get the full path of the file which i want to copy.

Error : Use of uninitialized value $full_path in print at New_UIbug_parser.pl line 70

One more thing i would like to add is files are placed in different directories.

Here is my porg:

use strict;
use warnings;
use File::Find;
use File::Path qw(make_path);
use File::Copy;


my $path = $ARGV[0];
my @UniqueAnr = ();
my %seen = ();
my $foundAnr;
find({ wanted => \&GetappropriateFile }, $path);
my @all_file;
my $file_name;
sub GetappropriateFile
{
  my $file = $_;

  if (-f $file && $file =~ /traces[_d+]/)
       {
              #my $line;
              $file_name = $File::Find::name;
              open(my $fh, "<", $file) or die "cannot open file:$!\n";
              my @all_lines =<$fh>;
              my $i=0;
              foreach my $check (@all_lines){
            if( $i < 10){
                  if($check =~ /Cmd line\:\s+com\.android\..*/){
                  #print"$file_name\n";
                   push(@all_file,$file);
                  #push(@all_file,$file_name);
                 }  
                 $i++;
            }
            else{
                 last;
                }
                  #print "$file\n";
                  close($fh);

        }

    }

} 

foreach my $all_anr_file (@all_file)
{
unless ($seen{$all_anr_file}) 
    {
       # if we get here, we have not seen it before
        push(@UniqueAnr, $all_anr_file);
        $seen{$all_anr_file}++;

    }
}




for my $anr_file (@UniqueAnr)
{
chomp($anr_file);
print "$anr_file\n";
my $full_path = <$path.*/$anr_file>;
print $full_path;
(my $dir = $anr_file) =~ s/\.[^.]+$//;
my $new_dir = File::Spec->catdir('\\\\star\Source_Temp\test', $dir);
#print"$new_dir\n";
#make_path($new_dir);
copy($full_path, $new_dir) or die "Copy failed for file $anr_file: $!";

}

Upvotes: 0

Views: 67

Answers (1)

simbabque
simbabque

Reputation: 54371

In your wanted sub you can access the full path of the file you are currently looking with $File::Find::name. See the docs of File::Find.

That means you don't really need all this stuff you have below your find. You can do it all in the wanted function.


Also note that you can use uniq from List::MoreUtils to replace your foreach and %seen solution.

Upvotes: 4

Related Questions