perl – 使用通配符移动文件
发布时间:2020-12-15 21:22:30 所属栏目:大数据 来源:网络整理
导读:是否可以使用文件::复制模块中的perl移动函数来使用通配符移动具有相同文件扩展名的多个公共文件? 到目前为止,如果我明确命名文件,我只能开始工作. 例如,我想做这样的事情: my $old_loc = "/share/cust/abc/*.dat";my $arc_dir = "/share/archive_dir/";
是否可以使用文件::复制模块中的perl移动函数来使用通配符移动具有相同文件扩展名的多个公共文件?
到目前为止,如果我明确命名文件,我只能开始工作. 例如,我想做这样的事情: my $old_loc = "/share/cust/abc/*.dat"; my $arc_dir = "/share/archive_dir/"; 现在,我可以像这样做一个文件: use strict; use warnings; use File::Copy; my $old_loc = "/share/cust/abc/Mail_2011-10-17.dat"; my $arc_dir = "/share/archive_dir/Mail_2011-10-17.dat"; my $new_loc = $arc_dir; #archive print "Moving files to archive...n"; move ($old_loc,$new_loc) || die "cound not move $old_loc to $new_loc: $!n"; 我想在perl程序结束时做什么,将所有这些名为* .dat的文件移动到一个存档目录. 解决方法
您可以使用Perl的
glob 运算符来获取需要打开的文件列表:
use strict; use warnings; use File::Copy; my @old_files = glob "/share/cust/abc/*.dat"; my $arc_dir = "/share/archive_dir/"; foreach my $old_file (@old_files) { my ($short_file_name) = $old_file =~ m~/(.*?.dat)$~; my $new_file = $arc_dir . $short_file_name; move($old_file,$new_file) or die "Could not move $old_file to $new_file: $!n"; } 这具有不依赖于系统调用的益处,该系统调用是不可移植的,依赖于系统的并且可能是危险的. 编辑:更好的方法是提供新目录而不是全新的文件名. (抱歉没想到这个!) move($old_file,$arc_dir) or die "Could not move $old_file to $new_file: $!n"; # Probably a good idea to make sure $arc_dir ends with a '/' character,just in case (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |