我有一个Perl脚本(move_file.pl
(,需要添加额外的功能。大于300KB的文件必须移动到另一个位置,然后脚本应发送电子邮件警告。
我如何读取文件的大小;较大的文件";去另一个地方?
当前移动脚本:
opendir(D, "$source_dir") or mail_die ("Fout bij lezen van directory $source_dir : $!" );
my @allfiles = sort( grep { -f "$source_dir/$_" } readdir D); #Lees alle bestanden uit de huidige werk directory
closedir(D);
my @filelist = grep { !/^.+(._cpcnv_)$/i } @allfiles; #Alle bestanden die NIET passen in het patroon *._cpcnv_ (Case insensitive)
foreach my $filename (@filelist)
{
my $source_file = "$source_dir/$filename";
my $target_file = "$target_dir/$filename";
rename $source_file , $target_file or mail_die ("Fout bij hernoemen van bestand $source_file naar $target_file : $! n" );
print "Bestand $source_file verplaatst naar $target_file.n";
}
debug "Succesvol beeindigd!n";
exit;
这将对较大的文件进行一些特殊处理,并在此过程中更改目标目录:
#!/usr/bin/env perl
use strict;
use warnings;
my($THRESHOLD) = 300 * 1024;
my($SOURCE_DIR) = "/tmp/src";
my($TARGET_DIR) = "/tmp/dst";
my($LARGE_DIR) = "/tmp/bigfiles";
foreach my $filename (@ARGV) {
next if -d $filename;
my($source_file) = "$SOURCE_DIR/$filename";
my($target_file) = "$TARGET_DIR/$filename";
my($size) = (stat($source_file))[7];
$target_file = mail_warn($source_file, $filename, $size) if ($size > $THRESHOLD);
print qq{"TARGET_DIR: "$target_file"n};
}
sub mail_warn
{
my($source_file, $filename, $size) = @_;
print qq{File "$source_file" too big! [$size]n};
my($new_target) = "$LARGE_DIR/$filename";
$new_target;
}
带有一些虚构数据的小样本:
$ /tmp/foo *
"TARGET_DIR: "/tmp/dst/aaa"
"TARGET_DIR: "/tmp/dst/bbb"
File "/tmp/src/bbb" too big! [3014026]
"TARGET_DIR: "/tmp/bigfiles/bbb"
"TARGET_DIR: "/tmp/dst/ccc"
File "/tmp/src/ddd" too big! [501130]
"TARGET_DIR: "/tmp/bigfiles/ddd"
"TARGET_DIR: "/tmp/dst/eee"