【发布时间】:2016-06-27 03:05:31
【问题描述】:
我正在寻找一种方法来重命名每个找到的 .seg 文件,以包含位于 .seg 文件上方两个目录的文件夹的名称。
例如我在
中找到了一个.seg文件
/data/test_all_runs/TestRun/Focus-HD753/QC/diffCoverage.seg
并且想重命名它
Focus-HD753.seg
一旦我重命名了文件,我想将它移动到
/data/test_all_runs/TestRun
或$ARGV[0]。这是我当前的代码:
#!/usr/bin/perl
use warnings;
use strict;
use File::Find;
use File::Spec;
my $home = "/data";
my @location_parts = ($home, 'test_all_runs');
push @location_parts, $ARGV[0] if @ARGV;
my $location = File::Spec->catdir(@location_parts);
my @moves;
my @vcf_moves;
sub find_seg {
my $F = $File::Find::name;
if ($F =~ /\.seg$/ ) {
my @path_parts = File::Spec->splitdir($F);
my $name = $path_parts[-3];
my $target = File::Spec->catdir($location, "$name.seg"); print $target;
push @moves, [ $F, $target ];
}
}
find({ wanted => \&find_seg, no_chdir => 1 }, $home);
while (@moves) {
my ($F, $target) = @{ shift @moves };
warn "$F -> $target";
rename $F, $target or warn "Can't move to $target";
}
sub find_vcf {
my $V = $File::Find::name;
if ($V =~ /(vcf$|oncomine\.tsv$)/ ) {
my @path_parts = File::Spec->splitdir($V);
print "The path_parts at 0 is #############".$path_parts[0]."\n";
print "The path_parts at -1 is #############".$path_parts[-1]."\n";
print "The path_parts at -2 is #############".$path_parts[-2]."\n";
print "The path_parts at -3 is #############".$path_parts[-3]."\n";
print "The path_parts at 1 is #############".$path_parts[1]."\n";
my $target_vcf = File::Spec->catdir($location, $path_parts[-1]); print $target_vcf;
push @vcf_moves, [ $V, $target_vcf ];
print "$V\n";
}
}
find({ wanted => \&find_vcf, no_chdir=>1}, $home);
while (@vcf_moves) {
my ($V, $target_vcf) = @{ shift @vcf_moves };
warn "$V -> $target_vcf";
rename $V, $target_vcf or warn "Can't move to $target_vcf";
}
【问题讨论】:
-
通常情况下,您应该告诉我们您当前的代码有什么问题……即您不理解的内容。
-
您要将其移动到“或 $ARGV[0]”还是“与 $ARGV[0] 连接”?
-
实际上,我需要将重命名的 .seg 文件移动到“test_all_runs”下一个目录的“TestRun”。此文件夹的名称将始终不同,但 /data/test_all_runs 将始终相同。谢谢
标签: perl