【问题标题】:Perl: How can I periodically move certain files from one folder to another?Perl:如何定期将某些文件从一个文件夹移动到另一个文件夹?
【发布时间】:2013-06-14 16:01:47
【问题描述】:

我需要每隔 5 分钟按字母顺序将具有特定文件名的文件从源文件夹移动到其特定的目标文件夹到另一个文件夹。

这就是我目前想出的......

#!/usr/bin/perl
use strict;
use warnings;
my $english = "sourcepath";
my $destination = "destination path";

#for(;;)
#{
opendir(DIR, $english) or die $!;
while (my $file = readdir(DIR))
    {
    next unless (-f "$english/$file");
    next unless ($file =~ m/english/);
    move ("$english/$file", "$destination");
    }
closedir (DIR);
#sleep 10;
#}  
exit 0;

现在的问题是,我无法按字母顺序一个一个地移动它们...任何指针?谢谢

【问题讨论】:

    标签: perl


    【解决方案1】:

    如果您想按字母顺序处理文件,请将它们排序。要获取他们的列表,您可以在列表上下文中使用readdir

    opendir(DIR, $english) or die $!;
    my @files = sort readdir DIR;
    for my $file (@files) {
        # ....
    }
    

    【讨论】:

      【解决方案2】:

      代替

      while (my $file = readdir(DIR))
      

      您可以按字母顺序获取文件,如下所示:

      for my $file (sort readdir(DIR))
      

      为使用strictwarnings 点赞。考虑阅读 perlstyle 以获取有关如何正确格式化代码的提示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-18
        • 2010-09-12
        • 1970-01-01
        相关资源
        最近更新 更多