perl - 在 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;
}
谢谢!
解决方案
我没有任何.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);
}
推荐阅读
- python - 这个python程序是做什么的?是老鼠吗?
- c++ - 服务器未从客户端接收。Linux TCP/IP 通信
- javascript - 焦点样式在反应组件中无法正常工作?
- python - 使用 systemd 运行的 Python 脚本无法启动 gedit
- tensorflow - 在深度学习中无法序列化大于 4 GiB 的字节对象
- r - R 函数 POSIXct 无法识别我的文件的时间格式
- ncurses - Ncurses中的换行符后如何防止光标回到屏幕的最左侧?
- video - 与 ffmpeg 的视频连接给出了不正确的持续时间
- javascript - 如何以角度选择输入类型文本的最后一个子项?
- javascript - 在没有任何 mapstatetoprops 的情况下连接 redux 并且仍然将东西作为道具