Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

cdrom stuff...

by Anonymous Monk
on Jun 06, 2000 at 23:04 UTC ( [id://16701]=perlquestion: print w/replies, xml ) Need Help??

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

In linux, how can I use perl to copy all the files from a cdrom to a directory I specify? I've been trying to figure this out for awhile and it's stumping me. Thanks, ope

Replies are listed 'Best First'.
RE: cdrom stuff...
by ivey (Beadle) on Jun 06, 2000 at 23:20 UTC
    1: why use perl?
    cp -aR /cdrom /directory
    should work, right?

    2: try

    perl -e 'system("cp -aR /cdrom /directory") or die "Cannot do it!"'

    3: You could iterate through the cdrom, using a set of recursive readdirs, and then File::Copy each file. But that would be pointless.

      that is only for suse. for most distros, it is /mnt/cdrom (after you've mounted /dev/cdrom) not /cdrom @///usr////((:::::bin:::::::::perl::::>
Re: cdrom stuff...
by athomason (Curate) on Jun 07, 2000 at 01:03 UTC
    Hmm, looks like the original article was moved. So here's a repost.

    If you want to do this in Perl, take a look at File::Tools. Specifically, the copydir function will do exactly what you need. If modules rub you wrong (though they really shouldn't), this might do for you:

    #!/usr/local/bin/perl -w use strict; use Carp; use File::Copy "cp"; # need two arguments scalar @ARGV == 2 or print("Usage: copy.pl <source_dir> <target_dir>"), exit; # record the bases of the source and target my ($source_dir, $target_dir) = @ARGV; CopyDir($source_dir, $target_dir); sub CopyDir { my ($source_dir, $target_dir) = @_; # read the source directory opendir (DIR, $source_dir) or croak "Couldn't open $source_dir: $! +"; my @list = readdir(DIR); # get a list of directories my @dirs = grep {!/^\./ && -d "$source_dir/$_"} @list; # get a list of real files + my @files = grep {-f "$source_dir/$_"} @list; # create the target if necessary (!-e $target_dir and !-d $target_dir and mkdir $target_dir) or croak "Couldn't mkdir $target_dir: $!"; # recursively copy subdirectories map CopyDir("$source_dir/$_", "$target_dir/$_"), @dirs; # copy real files last map cp("$source_dir/$_", "$target_dir/$_"), @files; }
Re: cdrom stuff...
by infoninja (Friar) on Jun 06, 2000 at 23:12 UTC
    You can either use a system() and call the appropriate copy command for your platform, or use the File::Copy module included with Perl
      File::Copy on its own only copies real files, not directories. See my reply further down for ways to do it with File::Tools or rolling your own version.
RE: cdrom question...
by le (Friar) on Jun 07, 2000 at 00:12 UTC
    cp -R /cdrom_mountpoint /target_dir

    This is the easy shell way, but you could put it into a system() call.
Re: cdrom question...
by athomason (Curate) on Jun 07, 2000 at 01:00 UTC
    If you want to do this in Perl, take a look at File::Tools. Specifically, the copydir function will do exactly what you need. If modules rub you wrong (though they really shouldn't), this might do for you:
    #!/usr/local/bin/perl -w use strict; use Carp; use File::Copy "cp"; # need two arguments scalar @ARGV == 2 or print("Usage: copy.pl <source_dir> <target_dir>"), exit; # record the bases of the source and target my ($source_dir, $target_dir) = @ARGV; CopyDir($source_dir, $target_dir); sub CopyDir { my ($source_dir, $target_dir) = @_; # read the source directory opendir (DIR, $source_dir) or croak "Couldn't open $source_dir: $! +"; my @list = readdir(DIR); # get a list of directories my @dirs = grep {!/^\./ && -d "$source_dir/$_"} @list; # get a list of real files + my @files = grep {-f "$source_dir/$_"} @list; # create the target if necessary (!-e $target_dir and !-d $target_dir and mkdir $target_dir) or croak "Couldn't mkdir $target_dir: $!"; # recursively copy subdirectories map CopyDir("$source_dir/$_", "$target_dir/$_"), @dirs; # copy real files last map cp("$source_dir/$_", "$target_dir/$_"), @files; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-24 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found