There is a lot of stuff wrong with the code you've posted. The fact that it won't compile is the least of it. First off, don't use backslashes in file paths -- Perl on Windows knows how to use forward slashes instead and do the right thing, and there are modules (look at File::Spec for starters) that will help you to manipulate file paths without worrying about the problems that backslashes can cause. In your case, note that in this string, at line 12 of the code:
$targetDirFull = 'C:\target\';
the second backslash is being interpreted as an escape for the final single-quote character, which means that there really is no close-quote on this line, and this is why it won't compile. (You did remember to use double backslashes elsewhere, at lines 16 and 17, but forward slashes for file paths is still a better way to go.

This bit is strange:

unless( chdir('C:\\') ) { print "Error 1..\n" ;} else { chdir('C:\\'); } unless (-e $targetDir) { print "creating directory \"Target\"...\n"; mkdir('Target',0777); }
This says that if you are able to do "chdir('C:/')", you do it a second time. Then if there is no file or directory named "multimedia" (this is the value assigned to $targetDir), you create a directory called "Target". (Huh?) You don't check whether the mkdir succeeds.

In this first use of readdir, you don't keep whatever file name was returned:

unless(readdir(ORIG)) { print "Error 2"; ## not a very informative report! } else { ...
When you read the rest of the directory entries into @allThings and loop through them, you don't check to see which ones are plain files, and which are subdirectories. (Trying to open and the diamond read operator on directories won't work.) Then for each file you try to process, you do another opendir on the output directory, which is quite unnecessary -- you don't even need to do that once.

In addition to File::Spec, I suggest you look up File::Copy as well -- it will help simplify what you are doing. Also try using the "-f" or "-d" file checks (these are like the "-e" check that you are already using) as you go through the entries in the input directory, and only deal with things that are data files.

Other advice (mostly stylistic): declare and initialize variables where they are needed, rather than putting a bunch of "my" declarations at the top; use "if" more than "unless"; see if you can use spaces instead of tabs when indenting lines (at least when you post code here), so the indents will appear as intended.


In reply to Re: this script normaly deplace files in a directory ! by graff
in thread this script normaly deplace files in a directory ! by boby_drack

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.