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

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', );
  • Comment on Re: directories and sub directories and copying or moving hundereds or thousands of files :)
  • 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 07:10 UTC
    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

        hmm. well if i am going to copy a file from dev_flash (folder) i need to read each file in the temp folder, then compare the file i get from searching directories against the filename in the files of the temp folder, then if it matches, copy it to destination.
        foreach my $element($array){ open my $tempfile, '<', $element; foreach $line(<$tempfile>){ if ($line =~ $file_to_copy){ copy ($file_to_copy, "copied_files/$element) } else{ print ''; } }
        i have no idea if that would even work :D
        To me, that is 1 to many of nested foreach loops, but it does look like it would work. ive had bad exeriences with foreach loops and subs :l