0

我想在网络路径中找到最新的子目录,并将最新子目录的全部内容复制到网络路径中的另一个文件夹中

我们在文件夹下有很多子文件夹\\10.184.132.202\projectdump我需要对子文件夹进行排序以进入最新文件夹并将整个内容复制到另一个文件夹中\\10.184.132.203\baseline

我正在使用下面提到的脚本,我能够在目录下列出最新修改的文​​件夹,但我不知道复制内容。

use File::stat;
use File::Copy qw(copy);
$dirname = '\\\\10.184.132.202\\projectdump\\Testing\\';
$destination = '\\\\10.184.132.203\\baseline\\Testing\\';
$timediff=0;
opendir DIR, "$dirname";
while (defined ($sub_dir = readdir(DIR)))
{
    if($sub_dir ne "." && $sub_dir ne "..")
    {
        $diff = time()-stat("$dirname/$sub_dir")->mtime;
        if($timediff == 0)
        {
            $timediff=$diff;
            $newest=$sub_dir;
        }
        if($diff<$timediff)
        {
            $timediff=$diff;
            $newest=$sub_dir;
        }
    }
}
print $newest,"\n";

open my $in, '<', $newest or die $!;
while (<$in>) {
    copy *, $destination; --------> Here i want to copy the entire contents of the $newest to $destination.
}
4

2 回答 2

1

Use File::Copy::Recursive. This is an optional module, but allows you to copy entire directory trees. Unfortunately, File::Copy::Recursive is not a standard Perl module, but you can install it via the cpan command.

If installing modules is a problem (sometimes it is), you can use the File::Find to go through the directory tree and copy files one at a time.

By the way, you can use forward slashes in Perl for Windows file names, so you don't have to double up on backslashes.

于 2012-08-31T14:15:20.650 回答
0

为什么不调用一个简单的 shell cmd 来查找最新的目录?我认为,这在shell中会简单得多......

my $newestdir=`ls -1rt $dirname|tail -n 1`;

在外壳中:

LATESTDIR=`ls -1rt $dirname|tail -n 1`
cp -r ${LATESTDIR}/* $destination/

Ups,我刚刚意识到您可能使用 Windows ......

将所有目录及其时间放入一个哈希中,然后对该哈希进行逆序排序以找到最新的

my ($newest) = sort {$hash{$b} cmp $hash{$a} keys %hash;

然后

opendir NDIR, "$newest";
while ($dir=<NDIR>) {
next if $dir eq '.' or $dir eq '..';
copy $dir, $destination;
}
于 2012-08-31T10:34:51.277 回答