我有这个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".
我不知道出了什么问题。我哪里错了?
答案 0 :(得分:8)
为什么重新发明轮子?有一个完全可以接受的INI file reader in CPAN。
> cat monsch.pl
#!/usr/bin/env perl -w
use strict;
use Config::INI::Reader;
my $filename = "/path/to/MonitorSchedule.ini";
my $ini = Config::INI::Reader->read_file($filename);
my $global_section = $ini->{'_'};
printf "schtasks.exe /create /tr %s /sc %s /d %s /st %s /tn %s /ru \"system\"\n"
, $global_section->{'taskrun'}
, $global_section->{'occurence'}
, $global_section->{'day'}
, $global_section->{'time'}
, $global_section->{'taskname'}
;
> ./monsch.pl
schtasks.exe /create /tr C:\monitor.bat /sc weekly /d Mon /st 01:00:00 /tn monitorschedule /ru "system"
答案 1 :(得分:4)
为了使代码更清晰,只需要一个“翻译”哈希:
use strict;
use warnings;
my $translation = {
"day" => "d",
"time" => "st",
"taskname" => "tn",
"taskrun" => "tr",
"occurence" => "sc"
};
my $command = "schtasks.exe /create ";
while (<DATA>) {
chomp $_;
my ($key, $value) = split(/=/);
$command .= "/$translation->{$key} $value ";
}
$command .= "/ru \"system\"";
__DATA__
day=Mon
time=01:00:00
occurence=weekly
taskname=monitorschedule
taskrun=C:\monitor.bat
答案 2 :(得分:3)