Hello Monks, I am struggling with a code. It is a very simple one, yet I simply CANNOT see where it fails exactly. Here is what I am trying to accomplish: Open a directory, 'scan' the files inside and get their extension (txt, html, pdf, you name it), create subfolders with these extensions, and then copy the files to these subfolders. The code seems to work because I manage to copy certain files. But this is where it infuriates me: It only copies certain files! I tried to change my directory structure, the file names, the number of files I wanted to copy, the code (in every possible way), but I just can't get it. So here it is:

use File::Copy; $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); 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"); open (DIRR,$dirr); copy ("$folder","$dir/Fichiers $2") ; close (DIRR); } $cpt++; if ($del=~m/^oui$/i) { unlink ("$dir/$folder"); } } } print"$cpt copies effectuees"; close (DIR);

I am no Perl expert, I am currently finishing my Master's degree in Translation and I really need this code to work to finally get it over with. What I am asking for is just to understand what goes wrong with it... Can you help me? Thanks a bunch! Ben


In reply to Perl - Copy files to a newly created folder by Benichouniev

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.