in reply to writtings files to a new folder

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.

Replies are listed 'Best First'.
Re^2: writtings files to a new folder
by rinceWind (Monsignor) on Jan 24, 2007 at 09:36 UTC
    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.

    That's not the complete picture, as non-Unix operating systems support POSIX syntax (i.e. Unix) besides their own native syntax. All operating systems that run perl support POSIX syntax, otherwise the perl build process wouldn't work. For a bizarre native syntax, have a look at VMS. There can be problems if you try and mix native and POSIX, though sometimes in Windows, you can get away with a mixture of slashes leaning both ways.

    My advice is to either stick to native syntax, with File::Spec and friends, or use POSIX syntax throughout. Beware that a user supplied filename might contain native syntax elements.

    For more on this, and portability in general, please refer to my talk slides - I have given this talk at several YAPCs.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)