【问题标题】:perl - loop through directory to find file.mdb and execute code if file.ldb not foundperl - 遍历目录以查找 file.mdb 并在未找到 file.ldb 时执行代码
【发布时间】:2012-04-26 20:02:41
【问题描述】:

我是一个初学者 PERL 程序员,我遇到了一个我无法克服的障碍。我整天在 perl.org 上阅读和重新阅读网络帖子和 Simon Cozens 的书,但似乎无法解决问题。

我使用下面代码的目的是遍历目录中的文件,并且当文件具有特定字符串时,验证名称是否存在具有不同扩展名的相同文件名,如果不存在,则给我打印文件名(稍后我将实现文件的删除,但现在我想确保它能够正常工作。)具体来说,我正在查找 .mdb 文件,并在检查没有关联的 .ldb 文件后,删除 . mdb 文件。

现在我的代码返回这个:

RRED_Database_KHOVIS.ldb
RRED_Database_KHOVIS.mdb
I will kill RRED_Database_KHOVIS.mdb

RRED_Database_mkuttler.mdb
I will kill RRED_Database_mkuttler.mdb

RRED_Database_SBreslow.ldb
RRED_Database_SBreslow.mdb
I will kill RRED_Database_SBreslow.mdb

我希望它只在没有关联的 .ldb 文件的 .mdb 文件之后返回“我将杀死...”。

我当前的代码如下。感谢您提供的任何帮助...

use strict;
use warnings;
use File::Find;
use diagnostics;

my $dir = "//vfg1msfs01ab/vfgcfs01\$/Regulatory Reporting/Access Database/";
my $filename = "RRED_Database";
my $fullname, my $ext;

opendir DH, $dir or die "Couldn't open the directory: $!";
while ($_ = readdir(DH)) {
my $ext = ".mdb";
if ((/$filename/) && ($_ ne $filename . $ext)) {
    print "$_ \n";
    unless (-e $dir . s/.mdb/.ldb/) {
        s/.ldb/.mdb/;
        print "I will kill $_ \n\n" ;
        #unlink $_ or print "oops, couldn't delete $_: $!\n";
    }
    s/.ldb/.mdb/;
}   
}

【问题讨论】:

    标签: perl


    【解决方案1】:

    在循环文件时,我喜欢反复使用“下一个”语句来确保我只查看我想要的内容。试试这个:

    use strict;
    use warnings;
    use File::Find;
    use diagnostics;
    
    my $dir = "//vfg1msfs01ab/vfgcfs01\$/Regulatory Reporting/Access Database/";
    my $filename = "RRED_Database";
    my $fullname, my $ext;
    
    opendir DH, $dir or die "Couldn't open the directory: $!";
    while ($_ = readdir(DH)) {
      my $ext = ".mdb";
    
      # Jump to next while() iteration unless the file begins 
      # with $filename and ends with $ext, 
      # and capture the basename in $1
      next unless $_ =~ m|($filename.*)$ext|;
    
      # Jump to next while() iteration if if the file basename.ldb is found
      next if -f $1 . ".ldb";
    
      # At this point, we have an mdb file with no matching ldb file
      print "$_ \n";
      print "I will kill $_ \n\n" ;
      #unlink $_ or print "oops, couldn't delete $_: $!\n";
    }
    

    【讨论】:

    • 感谢您。我也找到了一个答案 - 发布在下面 - 虽然你的代码看起来更干净。我现在就试试你的……
    • 在您的代码中,我在“next if -f $1 . ".ldb"; ... 我假设 ($filename.*) 是您如何将基本名称存储到 $1 变量,但我不完全理解这一点......我会研究,但有什么我遗漏的吗?
    • 不能再发布 7 个小时的答案!在stackoverflow上太年轻了:(
    • 再次感谢这段代码,我想通了,它基本上解决了问题,我的第一篇文章中可能还不清楚的一个小调整......重要的是我确切地学到了什么你的代码做得足够了,既知道它更精简了,而且它并没有完全满足我的需要
    【解决方案2】:

    虽然 stuart 的 anwser 使它更精简...我也能够让它与下面的代码一起工作...(我将 .mdb 更改为 .accdb,因为我现在正在处理不同的文件类型)

    use strict;
    use warnings;
    use File::Spec;
    use diagnostics;
    
    my $dir = "//vfg1msfs01ab/vfgcfs01\$/Regulatory Reporting/Access Database/";
    my $filename = "RRED_Database";
    my $ext;
    
    opendir DH, $dir or die "Couldn't open the directory: $!";
    while ($_ = readdir(DH)) {
        my $ext = ".accdb";
        if ((/$filename/)  && ($_ ne $filename . $ext)  && ($_ !~ /.laccdb/)) {
            # if file contains database name, is not the main database and is not a  locked version of db
            s/$ext/.laccdb/;
            unless (-e File::Spec->join($dir,$_)) { 
                s/.laccdb/$ext/;
                #print "I will kill $_ \n\n";
                unlink $_ or print "oops, couldn't delete $_: $!\n";
            }
            s/.laccdb/$ext/;
        }   
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-17
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      相关资源
      最近更新 更多