.seg
ファイルの上に2つのディレクトリを持つフォルダ名を含むように、それぞれのファイル名を変更する方法を探しています。例えばPerlで一括して名前を変更してファイルを移動する
私は
/data/test_all_runs/TestRun/Focus-HD753/QC/diffCoverage.seg
で.seg
ファイルを発見し、私は
/data/test_all_runs/TestRun
に移動したいファイルの名前を変更したら
Focus-HD753.seg
その名前を変更したいと思います
または$ARGV[0]
。あなたがFile::Basename
モジュールを使用することができます名前のファイル名の変更については
#!/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]"と連結しますか? – choroba
実際には、名前を変更した.segファイルを "test_all_runs"から1つ下の "TestRun"に移動する必要があります。このフォルダの名前は常に異なりますが、/ data/test_all_runsは常に同じです。ありがとう – user3781528