如何使用 Perl 正则表达式或其他方式解析 ini 文件?

发布于 2024-12-19 19:30:32 字数 1426 浏览 0 评论 0原文

我有这个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情痴 2024-12-26 19:30:32

为什么要重新发明轮子? CPAN 中有一个完全可以接受的 INI 文件阅读器

> 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"

Why re-invent the wheel? There is a perfectly acceptable 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"
甜警司 2024-12-26 19:30:32

为了使代码更清晰,只需有一个“翻译”哈希:

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

To make the code cleaner, just have a "translation" hash:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文