in reply to directories and sub directories and copying or moving hundereds or thousands of files :)

You can eliminate the if/elsif statements and reading the temp/dev_flash* files multiple times by creating a hash 'lookup' to decide which folders to copy the searched files into. For example ;

#!perl use strict; use warnings; use File::Find; use File::Copy qw(copy); use File::Path qw(make_path); use Data::Dump 'pp'; use Cwd; # search directory my $dir = $ARGV[0]; if ( not defined $dir ) { print "\nUsage: $0 dev_flash\n"; exit(0); } elsif (! -d $dir){ print "$dir does not exist\n"; exit(0); } # current working directory my $cwd = cwd(); # create lookup my %lookup=(); find(\&file_lookup,'temp'); #pp \%lookup; # scan files and copy my %report=(); find(\&file_copy,$dir); # report report_dev_flash('report.txt'); sub file_lookup { my $infile = $_; return if (-d $infile); open IN,'<',$infile or die "$! $infile"; my $count = 0; while (my $line = <IN>){ $line =~ s/^\s+//; $line =~ s/\s+$//; if ($line ne ''){ push @{$lookup{lc $line}}, $infile; ++$count; } } close IN; # create directory tree if ($count) { my $path = dupl_path($infile); make_path($path,{ verbose => 1 }); print "read $infile -- $count lines\n"; } } sub copy_path { return $cwd.'/copied_files/'.shift; } sub dupl_path { return copy_path(shift).'/duplicate'; } sub file_copy { my $file = $_; return if (-d $file); my $lcfile = lc $file; if ( exists $lookup{$lcfile} ){ for my $folder ( @{$lookup{$lcfile}} ){ my $path = copy_path($folder); if ( -e "$path/$file" ) { $path = dupl_path($folder); } #print "Copying $file, $path\n"; copy( $file, $path ) or die "$!"; push @{$report{$folder}}, $file; } } } sub report_dev_flash { my $outfile = shift; open OUT,'>',$outfile or die "$!"; for my $folder (sort keys %report){ print OUT "\n $folder |==================================>\n"; print OUT "\t$_\n" for @{$report{$folder}}; } close OUT; }

note :
There are 2 possible errors in the temp files ;
1 - dev_flash019 contains libgcm_sys.sprx.dex which does not exist
2 - libvpost.sprx appears in both dev_flash018 and dev_flash020

poj
  • Comment on Re: directories and sub directories and copying or moving hundereds or thousands of files :)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: directories and sub directories and copying or moving hundereds or thousands of files :)
by james28909 (Deacon) on Oct 04, 2014 at 23:04 UTC
    yes there are actually 5 or 6 files with the same name. that is why i made the "duplicates" folder, i know it would copy the duplicate file to the wrong directory, but i could easily distinguish which files was for which and i will add a filesize check in the next revision of the script.
    anyways, libgcm_sys.sprx.dex indeed does NOT exists. if you was to scrap the ".dex" extention, you will find that actual file. but the filenames are still being created as to which i need for the instance of dev_flash.... and it is a per firmware thing. so each new firmware might be different but atleast this will copy all known files that i need and only leave the few /newer/ ones to search for. then i can add those newer filenames to the correct file in temp folder :)