0

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?

tchrist
  • 78,834
  • 30
  • 123
  • 180
quinekxi
  • 879
  • 5
  • 14
  • 27
  • 2
    -1. You're obviously not running the same program you've pasted here since the program prints something that includes `/tn` and the output you show doesn't have that. Since the evidence doesn't match the story, this is not a good question. Please revise it. – Rob Kennedy Dec 06 '11 at 01:30
  • @RobKennedy no sir, the program doesn't print /tn, yes i do want to print that one, but that is the problem i have. – quinekxi Dec 06 '11 at 01:35
  • I don't think it's good to use regex here. Even if it's possible, it's needlessly complex and you will run into problems like this. – Dalmas Dec 06 '11 at 01:37
  • 1
    You program you included here (both before and after the edit) *will* include `/tn` in its output, as well as `/sc`, `/d`, and `/st`. The output you showed doesn't include that. Therefore, the output you showed doesn't come from that program. The problem is that you're not running the same program you're looking at in your editor. It has nothing to do with the content of the program or how to parse pseudo-INI files like this one. – Rob Kennedy Dec 06 '11 at 01:39
  • @RobKennedy That really is my problem, the output I am expecting is different. I don't know why /tn, /sc, /d and /st doesn't show. – quinekxi Dec 06 '11 at 01:46
  • And that's why I voted this question down. It's asking about something completely irrelevant to the problem. Delete the entire `while` loop and you'll still have the same problem, proving that your question is *not* about INI files or parsing at all. It's more fundamental than that. Your problem is with running the same program you're editing. You evidently have two copies of your script, or else you've forgotten so save the script you're editing. Pasting the script here won't help since your problem isn't with the *contents* of the file. You have an environment problem. – Rob Kennedy Dec 06 '11 at 01:56
  • @RobKennedy Thanks for making it all sense, I am also thinking about the environment setup. Cuz this code works perfectly in my local machine, but in another it just don't. – quinekxi Dec 06 '11 at 01:58

3 Answers3

8

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"
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
4

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
勿绮语
  • 9,170
  • 1
  • 29
  • 37
3

Use Config::IniFiles or Config::INI::Reader as suggested in How can I access INI files from Perl?

Community
  • 1
  • 1
SAN
  • 2,219
  • 4
  • 26
  • 32