Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: How do I copy files in a dir and continue into each subdir?

by pjf (Curate)
on Oct 03, 2001 at 15:07 UTC ( [id://116409]=note: print w/replies, xml ) Need Help??


in reply to How do I copy files in a dir and continue into each subdir?

File::Find is almost certainly what you're after, although the man page is a bit much to digest if you've never seen it before.

The big power of File::Find lays in the "wanted" function that you pass it. This can do pretty much anything you want. If (for example) you wanted to take each file you find and print it in rot13 to stdout, you could do something like this:

#!/usr/bin/perl -w use strict; use File::Find; # The shift here takes the first command line # argument and uses that as our path. find(\&rot_cat,shift); sub rot_cat { unless (open(INFILE,$_)) { warn "Can't open $_ - $!\n"; return; } local $/ = undef; # Slurp mode. my $contents = <INFILE>; # Rot13 our contents. $contents =~ tr/A-Za-z/N-ZA-Mn-za-m/; # Print our new file. print $contents; }
If you're copying files, your task is a little more tricky, as you'll need to create directories along the way, and make sure you know where in the source tree you are, and ensure you copy files to the appropriate plate in the destination tree.

Personally, if you're just copying trees of files around, I'd suggest you look at good ol' "cp -r" if you're on a UNIX flavoured system. It's much more understandable and obvious, and does its job extremely well. Indeed, doing a "cp -r" and then using File::Find will usually be a simplier problem to solve.

Cheers,
Paul

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://116409]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-26 08:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found