首页 > 解决方案 > 在 Perl 中读取和文本匹配 Outlook .msg 文件

问题描述

我在使用 Perl 匹配 .msg 文件时遇到问题。第一个代码块用于打印整个消息,但如果它包含某个字符串,我只需要文件名。

    use warnings;
    use strict;
    use Email::Outlook::Message;
    use Email::MIME;

    my $sourceDir = "c:/temp";

    open_msg("test.msg");

    sub open_msg {
       my $verbose = 0;
       my $msgFile = shift;
       my $origMsg = new Email::Outlook::Message "$sourceDir/$msgFile", $verbose or die "$!";
       my $mime = $origMsg->to_email_mime;
       print $mime->as_string;
       return ($origMsg);
    }

.msg 文件位于文件夹中(在 Windows 上)。我使用下面的代码来打印 .txt 文件的文件名,但我需要对 .msg 文件使用类似的东西。

#works for .txt files
my @files = glob "C:/temp";

foreach my $file (@files) {
open   (FILE, "$file");
while(my $line= <FILE> ){
    print "$file" if $line =~ /test_string/;
}
close FILE; 
} 

谢谢!

标签: perloutlookmsg

解决方案


我没有任何.msg文件可以方便地测试,但如何替换这一行:

print $mime->as_string;

......有这样的声明?

print $mime->as_string if $mime->as_string =~ /test_string/;

就“如果它包含test_string”而言,这应该可以解决问题。

如果您还想遍历整个*.msg文件列表,请尝试以下操作:

my @files = glob "C:/temp";

foreach my $file (@files) {
    open_msg($file);
}

推荐阅读