james28909 has asked for the wisdom of the Perl Monks concerning the following question:

i have written a script that will search a directory given as an arg and it will read a temp directory with files, inwhich these files contain filenames of the files to search for. When it finds the file, it puts it in the specified directory (which is obtained from the given filenames in the temp folder)

i borrowed the directory parsing part of the script from cdarke and modified it just a hair... so big thanks to that guy :)

Here is the full script, please help guide me as to what i may have done wrong, or if you download the script and files and figure out an alot faster or shorter version, please show me cz i baked my brain on this for a day and a half(tho i do feel like i am getting somewhat better lol)
use strict; use warnings; use File::Slurp; use File::Copy qw(copy); use File::Path qw(make_path); my @dev_flash000 = (); my @dev_flash001 = (); my @dev_flash002 = (); my @dev_flash003 = (); my @dev_flash004 = (); my @dev_flash005 = (); my @dev_flash006 = (); my @dev_flash007 = (); my @dev_flash008 = (); my @dev_flash009 = (); my @dev_flash010 = (); my @dev_flash011 = (); my @dev_flash012 = (); my @dev_flash013 = (); my @dev_flash014 = (); my @dev_flash015 = (); my @dev_flash016 = (); my @dev_flash017 = (); my @dev_flash018 = (); my @dev_flash019 = (); my @dev_flash020 = (); my @dev_flash021 = (); my @dev_flash022 = (); my @dev_flash023 = (); my @dev_flash024 = (); my @dev_flash025 = (); my @dev_flash026 = (); my @dev_flash027 = (); sub make_folders { my $tempdir = "temp"; opendir( DIR, $tempdir ); while ( my $tempfile = readdir(DIR) ) { next if -d $tempfile; make_path("copied_files/$tempfile/duplicate"); } } sub search_dir { 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 ) { search_dir($path); } else { extract_files( $dir, $file ); } } } sub extract_files { my $des = ''; my ( $dir, $file ) = @_; my $dev_flash000 = read_file('temp/dev_flash000'); my $dev_flash001 = read_file('temp/dev_flash001'); my $dev_flash002 = read_file('temp/dev_flash002'); my $dev_flash003 = read_file('temp/dev_flash003'); my $dev_flash004 = read_file('temp/dev_flash004'); my $dev_flash005 = read_file('temp/dev_flash005'); my $dev_flash006 = read_file('temp/dev_flash006'); my $dev_flash007 = read_file('temp/dev_flash007'); my $dev_flash008 = read_file('temp/dev_flash008'); my $dev_flash009 = read_file('temp/dev_flash009'); my $dev_flash010 = read_file('temp/dev_flash010'); my $dev_flash011 = read_file('temp/dev_flash011'); my $dev_flash012 = read_file('temp/dev_flash012'); my $dev_flash013 = read_file('temp/dev_flash013'); my $dev_flash014 = read_file('temp/dev_flash014'); my $dev_flash015 = read_file('temp/dev_flash015'); my $dev_flash016 = read_file('temp/dev_flash016'); my $dev_flash017 = read_file('temp/dev_flash017'); my $dev_flash018 = read_file('temp/dev_flash018'); my $dev_flash019 = read_file('temp/dev_flash019'); my $dev_flash020 = read_file('temp/dev_flash020'); my $dev_flash021 = read_file('temp/dev_flash021'); my $dev_flash022 = read_file('temp/dev_flash022'); my $dev_flash023 = read_file('temp/dev_flash023'); my $dev_flash024 = read_file('temp/dev_flash024'); my $dev_flash025 = read_file('temp/dev_flash025'); my $dev_flash026 = read_file('temp/dev_flash026'); my $dev_flash027 = read_file('temp/dev_flash027'); if ( $dev_flash000 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash000/$file" ) { push @dev_flash000, "$file\n"; my $des = "copied_files/dev_flash000/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash000, "$file\n"; my $des = "copied_files/dev_flash000/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash001 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash001/$file" ) { push @dev_flash001, "$file\n"; my $des = "copied_files/dev_flash001/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash001, "$file\n"; my $des = "copied_files/dev_flash001/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash002 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash002/$file" ) { push @dev_flash002, "$file\n"; my $des = "copied_files/dev_flash002/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash002, "$file\n"; my $des = "copied_files/dev_flash002/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash003 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash003/$file" ) { push @dev_flash003, "$file\n"; my $des = "copied_files/dev_flash003/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash003, "$file\n"; my $des = "copied_files/dev_flash003/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash004 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash004/$file" ) { push @dev_flash004, "$file\n"; my $des = "copied_files/dev_flash004/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash004, "$file\n"; my $des = "copied_files/dev_flash004/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash005 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash005/$file" ) { push @dev_flash005, "$file\n"; my $des = "copied_files/dev_flash005/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash005, "$file\n"; my $des = "copied_files/dev_flash005/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash006 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash006/$file" ) { push @dev_flash006, "$file\n"; my $des = "copied_files/dev_flash006/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash006, "$file\n"; my $des = "copied_files/dev_flash006/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash007 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash007/$file" ) { push @dev_flash007, "$file\n"; my $des = "copied_files/dev_flash007/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash007, "$file\n"; my $des = "copied_files/dev_flash007/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash008 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash008/$file" ) { push @dev_flash008, "$file\n"; my $des = "copied_files/dev_flash008/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash008, "$file\n"; my $des = "copied_files/dev_flash008/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash009 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash009/$file" ) { push @dev_flash009, "$file\n"; my $des = "copied_files/dev_flash009/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash009, "$file\n"; my $des = "copied_files/dev_flash009/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash010 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash010/$file" ) { push @dev_flash010, "$file\n"; my $des = "copied_files/dev_flash010/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash010, "$file\n"; my $des = "copied_files/dev_flash010/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash011 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash011/$file" ) { push @dev_flash011, "$file\n"; my $des = "copied_files/dev_flash011/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash011, "$file\n"; my $des = "copied_files/dev_flash011/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash012 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash012/$file" ) { push @dev_flash012, "$file\n"; my $des = "copied_files/dev_flash012/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash012, "$file\n"; my $des = "copied_files/dev_flash012/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash013 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash013/$file" ) { push @dev_flash013, "$file\n"; my $des = "copied_files/dev_flash013/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash013, "$file\n"; my $des = "copied_files/dev_flash013/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash014 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash014/$file" ) { push @dev_flash014, "$file\n"; my $des = "copied_files/dev_flash014/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash014, "$file\n"; my $des = "copied_files/dev_flash014/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash015 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash015/$file" ) { push @dev_flash015, "$file\n"; my $des = "copied_files/dev_flash015/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash015, "$file\n"; my $des = "copied_files/dev_flash015/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash016 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash016/$file" ) { push @dev_flash016, "$file\n"; my $des = "copied_files/dev_flash016/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash016, "$file\n"; my $des = "copied_files/dev_flash016/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash017 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash017/$file" ) { push @dev_flash017, "$file\n"; my $des = "copied_files/dev_flash017/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash017, "$file\n"; my $des = "copied_files/dev_flash017/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash018 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash018/$file" ) { push @dev_flash018, "$file\n"; my $des = "copied_files/dev_flash018/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash018, "$file\n"; my $des = "copied_files/dev_flash018/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash019 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash019/$file" ) { push @dev_flash019, "$file\n"; my $des = "copied_files/dev_flash019/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash019, "$file\n"; my $des = "copied_files/dev_flash019/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash020 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash020/$file" ) { push @dev_flash020, "$file\n"; my $des = "copied_files/dev_flash020/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash020, "$file\n"; my $des = "copied_files/dev_flash020/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash021 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash021/$file" ) { push @dev_flash021, "$file\n"; my $des = "copied_files/dev_flash021/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash021, "$file\n"; my $des = "copied_files/dev_flash021/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash022 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash022/$file" ) { push @dev_flash022, "$file\n"; my $des = "copied_files/dev_flash022/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash022, "$file\n"; my $des = "copied_files/dev_flash022/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash023 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash023/$file" ) { push @dev_flash023, "$file\n"; my $des = "copied_files/dev_flash023/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash023, "$file\n"; my $des = "copied_files/dev_flash023/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash024 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash024/$file" ) { push @dev_flash024, "$file\n"; my $des = "copied_files/dev_flash024/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash024, "$file\n"; my $des = "copied_files/dev_flash024/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash025 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash025/$file" ) { push @dev_flash025, "$file\n"; my $des = "copied_files/dev_flash025/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash025, "$file\n"; my $des = "copied_files/dev_flash025/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash026 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash026/$file" ) { push @dev_flash026, "$file\n"; my $des = "copied_files/dev_flash026/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash026, "$file\n"; my $des = "copied_files/dev_flash026/"; copy( "$dir/$file", $des ) or die "$!"; } } elsif ( $dev_flash027 =~ /\b$file\b/i ) { if ( -e "copied_files/dev_flash027/$file" ) { push @dev_flash027, "$file\n"; my $des = "copied_files/dev_flash027/duplicate/"; copy( "$dir/$file", $des ) or die "$!"; } else { push @dev_flash027, "$file\n"; my $des = "copied_files/dev_flash027/"; copy( "$dir/$file", $des ) or die "$!"; } } else { ###do nothing } } sub print_dev_flash { if (@dev_flash000 == 0){ print ''; }else{ print "\n\n dev_flash000\n|==================================>\ +n"; foreach my $filename(@dev_flash000){ print "\t$filename"; } } if (@dev_flash001 == 0){ print ''; }else{ print "\n\n dev_flash001\n|==================================>\ +n"; foreach my $filename(@dev_flash001){ print "\t$filename"; } } if (@dev_flash002 == 0){ print ''; }else{ print "\n\n dev_flash002\n|==================================>\ +n"; foreach my $filename(@dev_flash002){ print "\t$filename"; } } if (@dev_flash003 == 0){ print ''; }else{ print "\n\n dev_flash003\n|==================================>\ +n"; foreach my $filename(@dev_flash003){ print "\t$filename"; } } if (@dev_flash004 == 0){ print ''; }else{ print "\n\n dev_flash004\n|==================================>\ +n"; foreach my $filename(@dev_flash004){ print "\t$filename"; } } if (@dev_flash005 == 0){ print ''; }else{ print "\n\n dev_flash005\n|==================================>\ +n"; foreach my $filename(@dev_flash005){ print "\t$filename"; } } if (@dev_flash006 == 0){ print ''; }else{ print "\n\n dev_flash006\n|==================================>\ +n"; foreach my $filename(@dev_flash006){ print "\t$filename"; } } if (@dev_flash007 == 0){ print ''; }else{ print "\n\n dev_flash007\n|==================================>\ +n"; foreach my $filename(@dev_flash007){ print "\t$filename"; } } if (@dev_flash008 == 0){ print ''; }else{ print "\n\n dev_flash008\n|==================================>\ +n"; foreach my $filename(@dev_flash008){ print "\t$filename"; } } if (@dev_flash009 == 0){ print ''; }else{ print "\n\n dev_flash009\n|==================================>\ +n"; foreach my $filename(@dev_flash009){ print "\t$filename"; } } if (@dev_flash010 == 0){ print ''; }else{ print "\n\n dev_flash010\n|==================================>\ +n"; foreach my $filename(@dev_flash010){ print "\t$filename"; } } if (@dev_flash011 == 0){ print ''; }else{ print "\n\n dev_flash011\n|==================================>\ +n"; foreach my $filename(@dev_flash011){ print "\t$filename"; } } if (@dev_flash012 == 0){ print ''; }else{ print "\n\n dev_flash012\n|==================================>\ +n"; foreach my $filename(@dev_flash012){ print "\t$filename"; } } if (@dev_flash013 == 0){ print ''; }else{ print "\n\n dev_flash013\n|==================================>\ +n"; foreach my $filename(@dev_flash013){ print "\t$filename"; } } if (@dev_flash014 == 0){ print ''; }else{ print "\n\n dev_flash014\n|==================================>\ +n"; foreach my $filename(@dev_flash014){ print "\t$filename"; } } if (@dev_flash015 == 0){ print ''; }else{ print "\n\n dev_flash015\n|==================================>\ +n"; foreach my $filename(@dev_flash015){ print "\t$filename"; } } if (@dev_flash016 == 0){ print ''; }else{ print "\n\n dev_flash016\n|==================================>\ +n"; foreach my $filename(@dev_flash016){ print "\t$filename"; } } if (@dev_flash017 == 0){ print ''; }else{ print "\n\n dev_flash017\n|==================================>\ +n"; foreach my $filename(@dev_flash017){ print "\t$filename"; } } if (@dev_flash018 == 0){ print ''; }else{ print "\n\n dev_flash018\n|==================================>\ +n"; foreach my $filename(@dev_flash018){ print "\t$filename"; } } if (@dev_flash019 == 0){ print ''; }else{ print "\n\n dev_flash019\n|==================================>\ +n"; foreach my $filename(@dev_flash019){ print "\t$filename"; } } if (@dev_flash020 == 0){ print ''; }else{ print "\n\n dev_flash020\n|==================================>\ +n"; foreach my $filename(@dev_flash020){ print "\t$filename"; } } if (@dev_flash021 == 0){ print ''; }else{ print "\n\n dev_flash021\n|==================================>\ +n"; foreach my $filename(@dev_flash021){ print "\t$filename"; } } if (@dev_flash022 == 0){ print ''; }else{ print "\n\n dev_flash022\n|==================================>\ +n"; foreach my $filename(@dev_flash022){ print "\t$filename"; } } if (@dev_flash023 == 0){ print ''; }else{ print "\n\n dev_flash023\n|==================================>\ +n"; foreach my $filename(@dev_flash023){ print "\t$filename"; } } if (@dev_flash024 == 0){ print ''; }else{ print "\n\n dev_flash024\n|==================================>\ +n"; foreach my $filename(@dev_flash024){ print "\t$filename"; } } if (@dev_flash025 == 0){ print ''; }else{ print "\n\n dev_flash025\n|==================================>\ +n"; foreach my $filename(@dev_flash025){ print "\t$filename"; } } if (@dev_flash026 == 0){ print ''; }else{ print "\n\n dev_flash026\n|==================================>\ +n"; foreach my $filename(@dev_flash026){ print "\t$filename"; } } if (@dev_flash027 == 0){ print ''; }else{ print "\n\n dev_flash027\n|==================================>\ +n"; foreach my $filename(@dev_flash027){ print "\t$filename"; } } } my $dir = $ARGV[0]; chomp($dir); if ( not defined $dir ) { print "\nUsage: scan.pl dev_flash\n"; exit(0); } make_folders(); search_dir($dir); print_dev_flash();

What made this so hard for me was each file needed to go into a specific directory, thats where i got the idea to name the directory after the filenames in the temp folder ( if you download the files you will see what i mean ). i need to go back through and add my parenthesis around strings and variables, just to keep it clean ofcourse, but its working great on win7 64 bit with active state. But hopefully someone is able to crush these 700 something lines of code down some. i am proud of this and i even used warnings; and use strict; and no errors ;)

here are the files , just download them and unzip it anywhere and run the script. and you will see what it does.
  • Comment on directories and sub directories and copying or moving hundereds or thousands of files :)
  • Download Code

Replies are listed 'Best First'.
Re: directories and sub directories and copying or moving hundereds or thousands of files :)
by Laurent_R (Canon) on Oct 04, 2014 at 10:38 UTC
    Hi james28909,

    Larry Wall (the creator of Perl) often says and writes that one of the primary qualities of a programmer is laziness. To tell the truth, laziness sometimes requires some work, but the time gained is often really worth the effort. Your program is a pathological case of lack of laziness.

    There are several things you could do to divide your program's length by a factor of 10 or probably even more.

    my @dev_flash000 = (); my @dev_flash001 = (); # ... my @dev_flash027 = ();
    you could replace these 28 individual arrays by an array of arrays (AoA) or a hash of arrays (HoA):
    my %dev_flash; # an HoA for my $index (0..27) { @{$dev_flash{$index}} = (); }
    Actually, the initializing loop is not necessary, the structure will auto-vifify when you use it, but I wanted to show the whole thing for pedagogical purpose. So, this code could boil down to just 1 line.

    Now the initial part of your extract_file subroutine:

    my $dev_flash000 = read_file('temp/dev_flash000'); my $dev_flash001 = read_file('temp/dev_flash001'); # ... my $dev_flash001 = read_file('temp/dev_flash027');
    can be reduced to three lines using an array instead of 28 scalars:
    my @dev_content; # using a different variable name for clarity for my $i (0..27) { my $file_index = sprintf "%02i", $i; $dev_content[$_] = read_file("temp/dev_flash0$file_index") }
    Having done that, your huge repetitive if /elsif can be reduced to a dozen lines of code within a loop.

    Something like this:

    for my $i (0..27) { my $file_index = sprintf "%02i", $i; # ... } }
    I did not code the details because, on the one hand, I would prefer to leave it to you as an exercise, but mainly because all this would still be very inefficient. Notice that the two last snippets of code are within identical loops, so there is no point to build two loops, one is enough doing the two things. I leave it to you to do it. (But I would gladly try to help you if you encounter difficulties in doing it, and I am sure many other monks will also try to help you.)

    But more than that, it seems to me (but I am not sure) that you are doing a lot of useless work: you are calling the read_file subroutine 28 times, but, in effect you are using only 1 of them, and you do that each time you call the extract_file sub. I am not sure I understood everything you are doing, because you code is far too long for someone lazy like me ;-) to read it all in details, but there is probably a way to factor this out.

    Even if you don't want to use a HoA and an array the way I showed above and want to keep your data the way you have it, you could still cut down considerably your extract_file sub by calling another sub (within a loop or after each assignment) with the right parameters to do the comparisons.

    my $dev_flash000 = read_file('temp/dev_flash000'); compare($file, $dev_flash000, 'temp/dev_flash000'); my $dev_flash001 = read_file('temp/dev_flash001'); compare($file, $dev_flash001, 'temp/dev_flash001'); # ... my $dev_flash001 = read_file('temp/dev_flash027'); compare($file, $dev_flash027, 'temp/dev_flash027');
    and store your nested comparisons within the compare sub.

    Do yourself a favor: be lazy. ;-)

      yeah that is def something i am going to have to learn, array of array and hash of arrays. but the reason i did it like that was i needed to make sure the files found in dev_flash folder would be compared to all the filenames in files of the temp folder and logically that is what i worked out in my primitive brain lol. i do see your logic tho and i must learn how to use array of array. that initially was my first guess, but ofcourse i did not know much about it or how to use them so i made that script to write the loops for me and then copy pasta the code lol. and thanks for yoru input, this is exactly what i want (and to be lazy also lol), something to reflect on and learn. your examples uses the data i am working with so it makes it a little clearer to me. that might sound crazy to some but it does make it easier for me to understand lol. and thanks for yoru time and input sir :)
Re: directories and sub directories and copying or moving hundereds or thousands of files :)
by poj (Abbot) on Oct 04, 2014 at 18:15 UTC

    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 ;

    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
      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 :)
Re: directories and sub directories and copying or moving hundereds or thousands of files :)
by Anonymous Monk on Oct 04, 2014 at 06:42 UTC

    But hopefully someone is able to crush these 700 something lines of code down some.

    That somebody is you, write the next line

    my @devflash = ( 'temp/dev_flash000', 'temp/dev_flash001', 'temp/dev_flash002', 'temp/dev_flash003', 'temp/dev_flash004', 'temp/dev_flash005', 'temp/dev_flash006', 'temp/dev_flash007', 'temp/dev_flash008', 'temp/dev_flash009', 'temp/dev_flash010', 'temp/dev_flash011', 'temp/dev_flash012', 'temp/dev_flash013', 'temp/dev_flash014', 'temp/dev_flash015', 'temp/dev_flash016', 'temp/dev_flash017', 'temp/dev_flash018', 'temp/dev_flash019', 'temp/dev_flash020', 'temp/dev_flash021', 'temp/dev_flash022', 'temp/dev_flash023', 'temp/dev_flash024', 'temp/dev_flash025', 'temp/dev_flash026', 'temp/dev_flash027', );
      if yoru thinking about
      foreach my $file(@array){ ... get filenames ... }

      unless something liek this would have worked.
      foreach my $file(@array){ get_filenames($file); } sub get filename{ my ($file) = @_; foreach my $filename($file){ #get filename from file stored in @array + and find it. ... find file...; copy_file($filename) } sub copy_file{ my ($filename ) = @_; foreach ($filename) copy($filename, "copied_files/$dest"); #this is the tricky part, which + i will try to describe <.<
      See, the way the script works, is each of those files in in the temp folder, are how i rebuild the copied files directory. you could put a file called "HOTLANTA" in temp folder, and it would create a folder in copied files directory called "HOTLANTA". That was the only way i could think of to make it work like that. otherwise it just copied all the files to the root of the copied files folder (which is not what i wanted and is the reason i made a 700 line script lol. maybe i could have put a counter or something in there? or when reached EOF, i could have called the first sub again, i am not sure, but it absolutely depends on the array element to be able to copy the file back to the right folder. otherwise its just gonna copy all ove rthe place wont it?

      I did make this to build my huge loops tho xD
      open my $temp, '>', "loopfile" ; my $num = "000"; my $dev_flash= '$dev_flash'; my $dev = '@dev_flash'; foreach (0 .. 27){ print $temp "if ($dev$num == 0){\nprint '';\n}else{\n", 'print "\n\n'," dev_flash$num",'\n',"|============================= +=====>",'\n', '";', "\n", 'foreach my $filename(', "$dev$num", '){',"\n", 'print "\t$filename";', "\n", "\t",'}', "\n", '}', "\n\n"; + "\n +\n\tdev_flash018\n|==================================>\n", @dev_flash +018; $num0++; } my $num1 = 000; foreach(0 .. 27){ print $temp 'elsif (', "$dev_flash$num1", '=~ /\b$file\b/i ){', "\n", 'if (-e "./copied_files/', "dev_flash$num1", '/$file"){', "\n", #' print "$dir/$file match\n";', "\n", ' push ', "$dev$num1",',', '"$file\n";', "\n", ' my $des = "./copied_files/', "dev_flash$num1", '/duplicate/";', " +\n", ' copy ("$dir/$file", $des) or die "$!";', "\n", '}else{', "\n", ' push ', "$dev$num1",',', '"$file\n";', "\n", ' my $des = "copied_files/', "dev_flash$num1", '/";', "\n", ' copy ("$dir/$file", $des) or die "$!";', "\n", '}', "\n", '}', "\n"; $num1++; }
      that was tedious, but worth the effort. cz once i got this done i could make simple changes and update the original script easily.

        if yoru thinking about ... unless something liek this would have worked... See...otherwise its just gonna copy all ove rthe place wont it?

        We are rewriting  sub extract_files { to get it down to 20 lines

        Will you play along? Write the next line for real

        At worst it won't work and you will have wasted 10 minutes

        If I do the rewriting, you say "wow great" and then do your own thing anyway