将带有递增文件名的文件移动到零填充到Perl

时间:2015-07-27 20:45:28

标签: perl directory printf filenames move

我有0001_test.txt0100_test.txt的文件。我制作目录Dir1和Dir2。我想将文件0001_test.txt移动到0010_test.txt到目录Dir1。

我目前的剧本:

#!/usr/bin/perl

use strict;
use warnings;
use File::Copy;

my @files = <*.txt>;
my $files;
my $count;

for $files (@files) {
  ++$count;
  mkdir -p "Dir1";
  mkdir -p "Dir2";
   if ($count >= 1 && $count <= 10) {
   my $basename = printf "%04d" $count; 
   mv "($basename)_test.txt" "Dir1";
   }
}

这显然失败了,那么如何纠正这个呢?

2 个答案:

答案 0 :(得分:2)

你的问题很奇怪。 <style> .content {width:100%;} .content .block {float:left;width:20%} @media all and (max-width: 600px) { .content .block {width:25%} } @media all and (max-width: 500px) { .content .block {width:33.3%} } @media all and (max-width: 400px) { .content .block {width:50%} } </style> <div class="content"> <div class="block">Text</div> <div class="block">Text</div> <div class="block">Text</div> <div class="block">Text</div> <div class="block">Text</div> <div class="block">Text</div> <div class="block">Text</div> <div class="block">Text</div> <div class="block">Text</div> <div class="block">Text</div> <div class="block">Text</div> <div class="block">Text</div> <div class="block">Text</div> <div class="block">Text</div> <div class="block">Text</div> </div> 似乎与问题无关,我也不知道您期望Dir2如何在Perl程序中执行任何有用的操作。但是,这应该可以解决您的问题

mkdir -p "Dir1"

答案 1 :(得分:-1)

简易解决方案:

#!/usr/bin/perl

use strict;
use warnings;

use File::Copy 'move';

my @files = <*.txt>;
my $count;

mkdir "Dir1";
mkdir "Dir2";

my @dir = <Dir*>;

for my $files (@files) {
  ++$count;
   if ($count >= 1 && $count <= 9) {
   my $move_files = sprintf '%s/%04d_test.text', $dir[0], $count;
   move ($files, $move_files);
   }
}