[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Perl String Replacing

Posted on 2004-04-20
5
Medium Priority
?
480 Views
Last Modified: 2012-06-27
Hi All,

This is probably so simple that I'll kick myself...

I need a small Perl script which does the following:

Check if Lotus Notes 6 exists (check for existence of Notes.ini in C:\Program Files\Lotus\Notes\)
If not then check for existence of Notes.ini in C:\Lotus\Notes

Open Notes.ini file

See if a line similar to the following exists in the file:

SIEBELFORMPATH=SiebelMemo.nsf

or:

SIEBELFORMPATH=C:\Lotus\Notes\Data\SiebelMemo.nsf

(unfortunately the path in this line could have several variations)

If the line exists modify SiebelMemo.nsf to Siebel7Memo.nsf

If the line doesn't exists then add a line at the end of the file:

SIEBELFORMPATH=Siebel7Memo.nsf

I know I need to use either s/foo/bar/ or m/foobar/ or grep or something but my mind is blank and I can't seem to get it into gear. Any help much appreciated!!!

Gareth
0
Comment
Question by:g_tunley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 7

Assisted Solution

by:leflon
leflon earned 1600 total points
ID: 10867531
Hi g_tunley,
you may want to try the following little script.
you just have to modify the filenames in the open statements and the output goes to stdout. so you may have to redirect it to different file.

my $fnd = 0;
open(IN,"test.txt") || open(IN,"test1.txt") || die "no notes installed";

while(<IN>) {
    if ( /SIEBELFORMPATH=.*SiebelMemo.nsf/ ) {
        s/SiebelMemo.nsf/Siebel7Memo.nsf/g;
        $fnd = 1;
    }
    print;
}
if ( !$fnd ) {
    print "SIEBELFORMPATH=Siebel7Memo.nsf\n";
}

hth
leflon
0
 
LVL 4

Expert Comment

by:vi_srikanth
ID: 10867782
$ARGV[0]='C:\Program Files\Lotus\Notes\Notes.ini';
if (not -e $ARGV[0]) {$ARGV[0]='C:\Lotus\Notes\Notes.ini'}
if (not -e $ARGV[0]) {die "Notes not installed"}
undef($/); $^I='_in'; $_=<>;
s#SIEBELFORMPATH=SiebelMemo.nsf|SIEBELFORMPATH=C:\\Lotus\\Notes\\Data\\SiebelMemo.nsf|$#hi#;
print;
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 400 total points
ID: 10868657
Here my version:



@paths = ("C:\\Program Files\Lotus\\Notes", "C:\\Lotus\\Notes");
foreach $dir (@paths){
  if(-e "$dir\\notes.ini"){
    open (INI, "$dir\\notes.ini");
    $data = join "",<INI>;
    close INI;
    $data .= "\nSIEBELFORMPATH=$dir\\data\\Siebel7Memo.nsf\n" unless ($data=~ s/(SIEBELFORMPATH=.*)SiebelMemo.nsf/$1\Siebel7Memo.nsf/gi);
    open (INI, ">$dir\\notes.txt");
    print INI $data;
    close INI;
  }
}




0
 

Author Comment

by:g_tunley
ID: 10869362
Oops I seem to have got the accepted and assisted the wrong way round, though i think the points of been awarded properly.

For reference here is the final solution:

#!/usr/bin/perl

my $found = 0;

if (-e "C:/Lotus/Notes/notes.ini") {

      @args = ("ren", "C:\\Lotus\\Notes\\notes.ini", "notes.ini.backup");
      $return_code = system(@args);

      if ($return_code != 0) {
            print "Error creating backup file!!!\n";
            print "Error code returned was: " . $return_code . "\n";

            exit -1;
      }

      $original_filename = "C:/Lotus/Notes/notes.ini.backup";
      $new_filename = "C:/Lotus/Notes/notes.ini";

} elsif (-e "C:/Program Files/Lotus/Notes/notes.ini") {

      @args = ("ren", "C:\\Program Files\\Lotus\\Notes\\notes.ini", "notes.ini.backup");
      $return_code = system(@args);

      if ($return_code != 0) {
            print "Error creating backup file!!!\n";
            print "Error code returned was: " . $return_code . "\n";

            exit -1;
      }

      $original_filename = "C:/Program Files/Lotus/Notes/notes.ini.backup";
      $new_filename = "C:/Program Files/Lotus/Notes/notes.ini";

} else {
      print "Error can't find Lotus Notes you may need to edit the file manually!\n";

      exit -1;
}


open(IN, $original_filename);
open(OUT, ">$new_filename") || die "Error unable to create new notes.ini!!!";

while(<IN>) {
      if ( /SIEBELFORMPATH=.*SiebelEmail.nsf/ ) {
        s/SiebelEmail.nsf/Siebel7Email.nsf/g;
        $found = 1;
    }
    print OUT $_;
}

close(IN);

if ( !$found ) {
    print OUT "SIEBELFORMPATH=Siebel7Email.nsf\n";
}

close(OUT);

if (-e "C:/Lotus/Notes/Data/Siebel/SiebelEmail.nsf" && ! -e "C:/Lotus/Notes/Data/Siebel/Siebel7Email.nsf") {

      @args = ("copy", "\\\\server\\siebel7email.nsf", "C:\\Lotus\\Notes\\Data\\Siebel\\");
      $return_code = system(@args);

      if ($return_code != 0) {
            print "Error copying Siebel Email Database file!!!\n";
            print "Error code returned was: " . $return_code . "\n";

            exit -1;
      }

} elsif (-e "C:/Lotus/Notes/Data/SiebelEmail.nsf" && ! -e "C:/Lotus/Notes/Data/Siebel7Email.nsf") {

      @args = ("copy", "\\\\server\\siebel7email.nsf", "C:\\Lotus\\Notes\\Data\\");
      $return_code = system(@args);

      if ($return_code != 0) {
            print "Error copying Siebel Email Database file!!!\n";
            print "Error code returned was: " . $return_code . "\n";

            exit -1;
      }

} elsif (-e "C:/Program Files/Lotus/Notes/Data/Siebel/SiebelEmail.nsf" && ! -e "C:/Program Files/Lotus/Notes/Data/Siebel/Siebel7Email.nsf") {

      @args = ("copy", "\\\\server\\siebel7email.nsf", "C:\\Program Files\\Lotus\\Notes\\Data\\Siebel\\");
      $return_code = system(@args);

      if ($return_code != 0) {
            print "Error copying Siebel Email Database file!!!\n";
            print "Error code returned was: " . $return_code . "\n";

            exit -1;
      }

} elsif (-e "C:/Program Files/Lotus/Notes/Data/SiebelEmail.nsf" && ! -e "C:/Program Files/Lotus/Notes/Data/Siebel7Email.nsf") {

      @args = ("copy", "\\\\server\\siebel7email.nsf", "C:\\Program Files\\Lotus\\Notes\\Data\\");
      $return_code = system(@args);

      if ($return_code != 0) {
            print "Error copying Siebel Email Database file!!!\n";
            print "Error code returned was: " . $return_code . "\n";

            exit -1;
      }

} elsif (-e "C:/Program Files/Lotus/Notes/Data/" && ! -e "C:/Program Files/Lotus/Notes/Data/Siebel7Email.nsf") {

      @args = ("copy", "\\\\server\\siebel7email.nsf", "C:\\Program Files\\Lotus\\Notes\\Data\\");
      $return_code = system(@args);

      if ($return_code != 0) {
            print "Error copying Siebel Email Database file!!!\n";
            print "Error code returned was: " . $return_code . "\n";

            exit -1;
      }

} elsif (! -e "C:/Lotus/Notes/Data/Siebel7Email.nsf") {

      @args = ("copy", "\\\\server\\siebel7email.nsf", "C:\\Lotus\\Notes\\Data\\");
      $return_code = system(@args);

      if ($return_code != 0) {
            print "Error copying Siebel Email Database file!!!\n";
            print "Error code returned was: " . $return_code . "\n";

            exit -1;
      }

}

exit 0;
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question