#!/usr/bin/perl -w
use File::Copy;
use strict;
my $i= "0";
my $j= "1";
my $source_directory = $ARGV[$i];
my $target_directory = $ARGV[$j];
#print $source_directory,"\n";
#print $target_directory,"\n";
my@list=process_files ($source_directory);
print "remaninign files\n";
print @list;
# Accepts one argument: the full path to a directory.
# Returns: A list of files that reside in that path.
sub process_files {
my $path = shift;
opendir (DIR, $path)
or die "Unable to open $path: $!";
# We are just chaining the grep and map from
# the previous example.
# You'll see this often, so pay attention ;)
# This is the same as:
# LIST = map(EXP, grep(EXP, readdir()))
my @files =
# Third: Prepend the full path
map { $path . '/' . $_}
# Second: take out '.' and '..'
grep { !/^\.{1,2}$/ }
# First: get all files
readdir (DIR);
closedir (DIR);
for (@files) {
if (-d $_) {
# Add all of the new files from this directory
# (and its subdirectories, and so on... if any)
push @files, process_files ($_);
} else { #print @files,"\n";
# for(@files)
while(@files)
{
my $input= pop @files;
print $input,"\n";
copy($input,$target_directory);
}
}
# NOTE: we're returning the list of files
return @files;
}
}
これは基本的にソースから宛先にファイルをコピーしますが、ディレクトリをコピーする方法についてもガイダンスが必要です。ここで注意すべき主なことは、copy、move、および path 以外の CPAN モジュールが許可されていないことです。