To move a file, use the
File::Copy module, but make sure the move worked by looking at the return value. To check if a file exists somewhere, use the
-f FILENAME builtin. To go over all the files, use
for. Putting it all together:
use File::Copy;
our $VERBOSE = 1; # turn this off if you don't need the verbose messag
+es
for my $file (@files) {
if (-f "old/$file") {
print "old/$file -> new/$file\n" if $VERBOSE;
move("old/$file", "new/.") or die "move: old/$file: $!";
}
}
Note that if this encounters an error, it'll stop in the middle. Maybe in your application it's more right to push the erring filename to a @failed array and then print them all, retry, whatever. I don't know; it's just something to think about.
Also, I've assumed "/" is a correct filepath separator and in most cases it is, but if you want to write really portable code you'll want to look into File::Spec.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.