in reply to File copy based on conditions in two arrays
and i am wanting to copy from the 455dex directory to the 455cex directory OR copy the DEX directory to a temp folder while preserving cex filepath <.<use strict; use warnings; use diagnostics; use File::Copy::Recursive qw(fcopy rcopy); use File::Path qw(make_path remove_tree); use Digest::MD5; use Time::HiRes qw( time ); my $start = time(); my $dir1 = $ARGV[0]; my $dir2 = $ARGV[1]; if ( not defined $dir1 ) { print "\nUsage: rexscan.pl [folder]\n"; exit(0); } elsif ( not defined $dir2 ) { $dir2 = ''; } remove_tree( "CEX", "DEX" ); my @first_arg = (); my @second_arg = (); my @dex_md5_array = (); my @cex_md5_array = (); my $countera = 0; my $counterb = 0; first_arg($dir1); second_arg($dir2); get_files(); sub first_arg { my ($dir) = @_; my ($dh); if ( !opendir( $dh, $dir ) ) { return; } while ( my $file = readdir($dh) ) { next if ( -d $file ); my $path = "$dir/$file"; if ( -d $path ) { first_arg("$path"); } else { push( @first_arg, "$dir/$file" ); } } } sub second_arg { my ($dir) = @_; my ($dh); if ( !opendir( $dh, $dir ) ) { return; } while ( my $file = readdir($dh) ) { next if ( -d $file ); my $path = "$dir/$file"; if ( -d $path ) { second_arg("$path"); } else { push( @second_arg, "$dir/$file" ); } } } sub get_files { for my $element1 (@first_arg) { next if ( -d $element1 ); $element1 =~ m#^(.*?)([^/]*)$#; my ( $dex_directory, $dex_temp_file) = ( $1, $2 ); my $dex = "$dex_directory$dex_temp_file"; open (my $dex_file, '<', $dex)|| die "line 90 $!"; my $dex_md5 = Digest::MD5->new->addfile($dex_file)->hexdigest; + push @dex_md5_array, "$dex_md5 $dex_directory $dex_temp_file\n +"; } for my $element2 (@second_arg) { next if -d $element2; $element2 =~ m#^(.*?)([^/]*)$#; my ( $cex_directory, $cex_temp_file ) = ( $1, $2 ); my $cex = "$cex_directory$cex_temp_file"; open (my $cex_file, '<', $cex) || die "line 101"; my $cex_md5 = Digest::MD5->new->addfile($cex_file)->hexdigest; push @cex_md5_array, "$cex_md5 $cex_directory $cex_temp_file"; } for my $cex_md5_array (@cex_md5_array) { my ( $md5c, $cpath, $cfile ) = split /\s+/, ($cex_md5_array); + #cex md5 filepath and file stored here chomp ( $md5c, $cpath, $cfile ); for my $dex_md5_array (@dex_md5_array) { my ( $md5d, $dpath, $dfile ) = split (/\s+/,$dex_md5_array +); #dex md5 filepath and file stored here chomp ( $md5d, $dpath, $dfile ); if ($md5c ne $md5d && $cfile eq $dfile){ make_path("DEX"); fcopy("$dpath$dfile", "DEX/$cpath"); #or use this to copy directly to the other directory. #fcopy("$dpath$dfile", "$cpath"); } else { print ''; } } } } my $end_run = time(); my $end = time(); my $runtime = sprintf( "%.5f", $end - $start ); print "This script took $runtime seconds to execute\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: File copy based on conditions in two arrays
by kzwix (Sexton) on Oct 09, 2014 at 11:11 UTC | |
by james28909 (Deacon) on Oct 09, 2014 at 21:13 UTC | |
by kzwix (Sexton) on Oct 10, 2014 at 12:29 UTC |