home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magazyn Amiga Shareware Floppies
/
ma64.dms
/
ma64.adf
/
AmiPhone
/
EditTextFile.rexx
< prev
next >
Wrap
OS/2 REXX Batch file
|
1996-07-07
|
4KB
|
141 lines
/*
EditTextFile.rexx v1.1 by Jeremy Friesner
An ARexx script to cleanly modify text config
lines. Any line beginning with sReplaceMe in
the File fModifyMe will be replaced with the line sWithMe.
If no such thing as sReplaceMe is found in the
file, then sWithMe will be added to the end of
the file.
This script should never lose the original file:
the first thing it does it rename the file to
filename.bak and then reconstructs the modified
version at the original file name by reading the
.bak file. If an error occurs, it will restore
filename.bak back to the original filename.
Any ^ signs in the sReplaceMe or sWithMe args will
be turned into space characters. (Although sReplaceMe
doesn't actually need these, because it's the last
arg and thus spaces will be included in it anyway.
Isn't Rexx argument passing fun?)
*/
parse arg fModifyMe sReplaceMe sWithMe
address COMMAND
sReplaceMe = ChangeToSpaces(sReplaceMe)
sWithMe = strip(ChangeToSpaces(sWithMe))
fBackupFile = fModifyMe || ".bak"
say "File to be modified = [" || fModifyMe || "]"
say "Backup file = [" || fBackupFile || "]"
say "String to add/replace = [" || sReplaceMe || "]"
say "add/Replace with = [" || sWithMe || "]"
keylength = length(sReplaceMe)
/* First, make sure rexxsupport.library is available. */
call addlib("rexxsupport.library", 0, -30, 0)
/* Delete any .bak file that now exists */
call delete(fBackupFile)
/* Rename the original filename to filename.bak */
if (rename(fModifyMe, fBackupFile) == 0) then do
say "Error: Couldn't rename " || fModifyMe || " to " || fBackupFile
exit(30)
end
BFileRenamed = 1
/* Default == haven't found the line we want to replace yet */
BFoundOurLine = 0
/* Open the backup file for reading */
if (open('oldfile',fBackupFile,'R') == 0) then do
say "Couldn't open backup file " || fBackupFile
call error
end
/* and the new file for writing */
if (open('newfile',fModifyMe,'W') == 0) then do
say "Couldn't open output file " || fModifyMe
call error
end
/* Scan the old file, writing the new one */
do while (EOF('oldfile') == 0)
nextline = readln('oldfile')
sCheckPart = left(nextline,keylength)
if ((BFoundOurLine == 0)&(sCheckPart = sReplaceMe)) then
do
/* don't write out this line... write out our substitute instead! */
say "Found: [" || nextline || "] replacing with [" || sWithMe || "]"
call writeln('newfile',sWithMe)
BFoundOurLine = 1
end
else do
call writeln('newfile',nextline)
end
end
/* If we never found our line to replace, then just add our
line at the end of the file. */
if (BFoundOurLine == 0) then call writeln('newfile',sWithMe)
call close('oldfile')
call close('newfile')
/* success! */
successMessage = "File " || fModifyMe || " successfully modified (" || sReplaceMe || ") -> " || "(" || sWithMe || ")."
say successMessage
/* Show calling app that we succeeded */
address COMMAND 'echo ' || successMessage || ' >t:edit_text_succeeded'
exit(0)
/* Changes all occurrences of the character '^' to spaces in the string.
Needed to get around ARexx's lame argument parsing. I think.
*/
ChangeToSpaces: procedure
parse arg sOrig
sNew = ""
do while (length(sOrig) > 0)
cChar = left(sOrig,1)
if (cChar == "^") then cChar = " "
sNew = sNew || cChar
sOrig = right(sOrig,length(sOrig)-1)
end
return sNew
/* Replaces original file if an error occurs */
error:
say "An error occured!"
call close('oldfile')
call close('newfile')
if (BFileRenamed == 1) then do
say "Attempting to restore the original file " || fModifyMe
/* Delete the incomplete file that now exists */
call rename(fModifyMe, fModifyMe || ".tobedeleted")
/* Rename filename.bak back to filename */
if (rename(fBackupFile, fModifyMe) == 0) then do
call rename(fModifyMe || ".tobedeleted", fModifyMe)
say "Error: Couldn't rename " || fBackupFile || " to " || fModifyMe || ", aborting now."
exit(30)
end
call delete(fModifyMe||".tobedeleted")
say "Recovery of original file " || fModifyMe || " was successful."
end
exit(30)