in reply to Re: Perl - Copy files to a newly created folder
in thread Perl - Copy files to a newly created folder

Alexander, thank you for you answer! I changed open to opendir, it was a mistake indeed... But it did not solve the problem. I also added 'use warnings' and 'or die' everywhere but I don't get any error messages when I run the program. I see 'x files have been copied', where x is the number of files I have, which means the loop runs once per file as expected. However, sometimes it copies, sometimes it does not. And I don't know why...
Ben

  • Comment on Re^2: Perl - Copy files to a newly created folder

Replies are listed 'Best First'.
Re^3: Perl - Copy files to a newly created folder
by afoken (Chancellor) on Apr 18, 2015 at 16:55 UTC
    I changed open to opendir, it was a mistake indeed... But it did not solve the problem.

    Of course not. Neither open nor opendir makes any sense around copy.

    I don't get any error messages when I run the program. I see 'x files have been copied', where x is the number of files I have, which means the loop runs once per file as expected. However, sometimes it copies, sometimes it does not. And I don't know why...

    Show your current code.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Alexander, this is my current code: (after I changed what I told you in my previous message, and it did not change anything)

      #!/usr/bin/perl -w use File::Copy; use warnings; $dir="C:/Users/Beni/Documents/Master 2/_DOSSIERS/_Perl/dossiertest"; $cpt=0; print"Souhaitez-vous supprimer les fichiers copies du dossier racine u +ne fois la copie terminee ? (oui/non) : "; chomp ($del = <>); opendir(DIR,$dir) or die $! ; while($folder=readdir (DIR)) { if ( -f "$dir/$folder") { $folder =~m/(.+)?\.(.+)?/; if ( -d "$dir/Fichiers $2") { copy ("$folder","$dir/Fichiers $2/$folder"); } else { $dirr = $dir . '/' . "Fichiers " . $2; mkdir ("$dirr"); copy ("$folder","$dir/Fichiers $2") or die $! ; } $cpt++; if ($del=~m/^oui$/i) { unlink ("$dir/$folder") or die $! ; } } } print"$cpt copies effectuees"; close (DIR);
        • use strict; is still missing
        • only one copy has an error check, the one that is less likely to be executed.
        • mkdir has no error check.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)