如何使用 Perl 正则表达式或其他方式解析 ini 文件?
我有这个ini文件。
day=Mon
time=01:00:00
occurence=weekly
taskname=monitorschedule
taskrun=C:\monitor.bat
我想要这样的输出,以便在 Windows XP 中创建新的计划作业。
schtasks.exe /create /tr c:\monitor.bat /sc weekly /d Mon /st 01:00:00 /tn monitorschedule /ru "system"
我尝试过这样的事情:
my $file = 'C:\strawberry\perltest\ini file\MonitorSchedule.ini';
my $d;
my $t;
my $o;
my $n;
my $r;
my $u = '"system"';
open (TEST, "<", $file) or die $!; # open ini file
while(<TEST>) # read all lines of the ini file
{
if($_ =~ m/day/)
{
my $day = $_;
my @days = split('=', $day);
$d = $days[1];
}
if($_ =~ m/time/)
{
my $time = $_;
my @times = split('=', $time);
$t = $times[1];
}
if($_ =~ m/occurence/)
{
my $occurrence = $_;
my @occurrences = split('=', $occurrence);
$o = $occurrences[1];
}
if($_ =~ m/taskname/)
{
my $taskname = $_;
my @tasknames = split('=', $taskname);
$n = $tasknames[1];
}
if($_ =~ m/taskrun/)
{
my $taskrun = $_;
my @taskruns = split('=', $taskrun);
$r = $taskruns[1];
}
}
close TEST;
print "schtasks.exe /create /tr $r /sc $o /d $d /st $t /tn $n /ru $u";
不幸的是输出不是我想要的。
schtasks.exe /create /tr C:\monitor.bat /ru "system".
我不知道出了什么问题。我哪里错了?
I have this ini file.
day=Mon
time=01:00:00
occurence=weekly
taskname=monitorschedule
taskrun=C:\monitor.bat
I want to have an output like this for create a new schedule job in a windows xp.
schtasks.exe /create /tr c:\monitor.bat /sc weekly /d Mon /st 01:00:00 /tn monitorschedule /ru "system"
I have tried something like this:
my $file = 'C:\strawberry\perltest\ini file\MonitorSchedule.ini';
my $d;
my $t;
my $o;
my $n;
my $r;
my $u = '"system"';
open (TEST, "<", $file) or die $!; # open ini file
while(<TEST>) # read all lines of the ini file
{
if($_ =~ m/day/)
{
my $day = $_;
my @days = split('=', $day);
$d = $days[1];
}
if($_ =~ m/time/)
{
my $time = $_;
my @times = split('=', $time);
$t = $times[1];
}
if($_ =~ m/occurence/)
{
my $occurrence = $_;
my @occurrences = split('=', $occurrence);
$o = $occurrences[1];
}
if($_ =~ m/taskname/)
{
my $taskname = $_;
my @tasknames = split('=', $taskname);
$n = $tasknames[1];
}
if($_ =~ m/taskrun/)
{
my $taskrun = $_;
my @taskruns = split('=', $taskrun);
$r = $taskruns[1];
}
}
close TEST;
print "schtasks.exe /create /tr $r /sc $o /d $d /st $t /tn $n /ru $u";
Unfortunately the output is not what I want.
schtasks.exe /create /tr C:\monitor.bat /ru "system".
I don't know what's wrong. Where am I wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么要重新发明轮子? CPAN 中有一个完全可以接受的 INI 文件阅读器。
Why re-invent the wheel? There is a perfectly acceptable INI file reader in CPAN.
为了使代码更清晰,只需有一个“翻译”哈希:
To make the code cleaner, just have a "translation" hash:
使用 Config::IniFiles 或 Config::INI::Reader 如 如何从 Perl 访问 INI 文件?
Use Config::IniFiles or Config::INI::Reader as suggested in How can I access INI files from Perl?