Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Platform-independent copying multiple files

by tomazos (Deacon)
on Oct 18, 2005 at 09:39 UTC ( #500916=perlquestion: print w/replies, xml ) Need Help??

tomazos has asked for the wisdom of the Perl Monks concerning the following question:

Let's suppose we have a $source glob string like:

  • '/home/fred/*.p
  • 'D:\\something\\*.tas'
  • 'rel/foo.*'

and a $destination like

  • '/home/anna/here'
  • '/home/anna/here/'
  • 'E:\\whatever/bing'
  • 'ting\\here\\'

I want to copy all the files from $source to $destination in a platform-independent way

At the moment the best I can think of is:

system("cp $source $destination");

Is there a better way?


Replies are listed 'Best First'.
Re: Platform-independent copying multiple files
by Skeeve (Parson) on Oct 18, 2005 at 09:47 UTC
    perldoc File::Copy

Re: Platform-independent copying multiple files
by Delusional (Beadle) on Oct 18, 2005 at 10:57 UTC
    cp isn't platform-independent. I beleive copy is aliased on linux systems these days, but don't rely on that.

    Your probibily better off going with Skeeve's recomendation to use File::Copy. But then again, you could go hard core, and manually open the files and bitstream binary copy through Perl, but is that really necessary (let alone safe)?
Re: Platform-independent copying multiple files
by ambrus (Abbot) on Oct 18, 2005 at 13:36 UTC

    File::Copy is a core module to copy files.

    You can use globs or the File::Glob module to find files matching the glob.

      I've used:
      my @all_matches = glob "*.txt"; my @files = grep {-f} @all_matches;
      for the glob part... :)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://500916]
Approved by Skeeve
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2023-12-10 08:09 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (39 votes). Check out past polls.