g_tunley
asked on
Perl String Replacing
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\No tes\Data\S iebelMemo. 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
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.
or:
SIEBELFORMPATH=C:\Lotus\No
(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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.i ni", "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.in i") {
@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.in i.backup";
$new_filename = "C:/Program Files/Lotus/Notes/notes.in i";
} 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=.*SiebelEm ail.nsf/ ) {
s/SiebelEmail.nsf/Siebel7E mail.nsf/g ;
$found = 1;
}
print OUT $_;
}
close(IN);
if ( !$found ) {
print OUT "SIEBELFORMPATH=Siebel7Ema il.nsf\n";
}
close(OUT);
if (-e "C:/Lotus/Notes/Data/Siebe l/SiebelEm ail.nsf" && ! -e "C:/Lotus/Notes/Data/Siebe l/Siebel7E mail.nsf") {
@args = ("copy", "\\\\server\\siebel7email. nsf", "C:\\Lotus\\Notes\\Data\\S iebel\\");
$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/Siebe lEmail.nsf " && ! -e "C:/Lotus/Notes/Data/Siebe l7Email.ns f") {
@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/Sie bel/Siebel Email.nsf" && ! -e "C:/Program Files/Lotus/Notes/Data/Sie bel/Siebel 7Email.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/Sie belEmail.n sf" && ! -e "C:/Program Files/Lotus/Notes/Data/Sie bel7Email. 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/Sie bel7Email. 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/Siebe l7Email.ns f") {
@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;
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.i
$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.
$new_filename = "C:/Lotus/Notes/notes.ini"
} elsif (-e "C:/Program Files/Lotus/Notes/notes.in
@args = ("ren", "C:\\Program Files\\Lotus\\Notes\\notes
$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.in
$new_filename = "C:/Program Files/Lotus/Notes/notes.in
} 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=.*SiebelEm
s/SiebelEmail.nsf/Siebel7E
$found = 1;
}
print OUT $_;
}
close(IN);
if ( !$found ) {
print OUT "SIEBELFORMPATH=Siebel7Ema
}
close(OUT);
if (-e "C:/Lotus/Notes/Data/Siebe
@args = ("copy", "\\\\server\\siebel7email.
$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/Siebe
@args = ("copy", "\\\\server\\siebel7email.
$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/Sie
@args = ("copy", "\\\\server\\siebel7email.
$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/Sie
@args = ("copy", "\\\\server\\siebel7email.
$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/Sie
@args = ("copy", "\\\\server\\siebel7email.
$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/Siebe
@args = ("copy", "\\\\server\\siebel7email.
$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;
if (not -e $ARGV[0]) {$ARGV[0]='C:\Lotus\Notes\
if (not -e $ARGV[0]) {die "Notes not installed"}
undef($/); $^I='_in'; $_=<>;
s#SIEBELFORMPATH=SiebelMem
print;