home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga MA Magazine 1998 #3
/
amigamamagazinepolishissue1998.iso
/
bazy
/
quickfile3
/
arexx
/
findreplace.quickfile
< prev
next >
Wrap
Text File
|
1995-11-19
|
4KB
|
218 lines
/*
FindReplace.QuickFile
Replaces an existing string with a new string in all records
in the current index (or selection)
Author: Alan Wigginton
Date: July 1995
*/
options results
if ~open(console,"con:20/40/460/120/SetField","W") then
do
say "Could not open window"
exit(20)
end
"query field fld" /* get all the field names in stem fld. */
"setindex" /* get the current index name */
ixname = result
"query index ixfld '"ixname"'" /* get the index field names */
if rc > 0 then
call ErrorProc "Error getting index details"
call writeln(console, "Find and replace string")
call writeln(console, "Press RETURN with no value to exit")
/*
Get the field name. Keep trying until the user gets it right
or they give up (RETURN with no data)
*/
err = "Y"
do while err = "Y"
call writeln(console, "What is the field to be changed?")
ReqFld = upper(readln(console))
if ReqFld = "" then
exit
call CheckFld
end
call CheckIndex
/*
Get the value to set the field to
*/
err = "Y"
do while err = "Y"
call writeln(console, "Search for what?")
SrchStr = readln(console)
if SrchStr ~= "" then
err = "N"
else
call writeln(console, "Must have a search string")
end
call GetYesNo "Match case y/n?", "n"
CaseSens = result
call writeln(console, "Replace" SrchStr "with what?")
RepStr = readln(console)
/*
Confirm operation and number of records to be updated
*/
"numrecs" /* ask quickfile for the number of records */
numrecs = result
if ixname = "Selected" then
ixtype = "Selection"
else
ixtype = "all"
call writeln(console, "About to replace" SrchStr)
call writeln(console, "with" RepStr "in" ixtype "records")
call GetYesNo "Continue y/n?", "y"
if result ~= "Y" then
do
call writeln(console, "Request cancelled - press any key")
ans = readln(console)
exit
end
/*
Change all the records
*/
count = 0
srchlen = length(SrchStr)
replen = length(RepStr)
if CaseSens = "N" then
SrchStr = upper(SrchStr)
"next -9999"
eof = "N"
do while eof = "N"
"getfield '"ReqFld"'"
if CaseSens = "N" then
pos = Index(upper(result), SrchStr)
else
pos = Index(result, SrchStr)
if pos > 0 then
do
newstr = delstr(result, pos, srchlen)
newstr = insert(RepStr, newstr, pos - 1)
"putfield '"ReqFld"' '"newstr"'"
if rc > 0 then
do
call writeln(console, "Error placing" newstr "into" ReqFld)
call ErrorProc count "records updated"
end
"updrec" /* commit the updated record */
if rc > 0 then
call ErrorProc "Error updating record"
count = count + 1
end
"next"
if rc > 0 then
eof = "Y"
end
/*
Tell the user that we completed it OK
*/
call writeln(console, count "records updated")
call writeln(console, "Press RETURN to continue")
ans = readln(console)
exit
/*
Check that the field name is valid
*/
CheckFld:
do i = 1 to fld.0
if ReqFld = fld.i then
do
err = "N"
return
end
end
call writeln(console, ReqFld "is unknown")
return
CheckIndex:
ans = "Y"
do i = 1 to ixfld.0
if ReqFld = ixfld.i then
ans = "N"
end
if ans = "N" then
do
call writeln(console, ReqFld "is used in current index")
call writeln(console, "results may be unpredictable")
call writeln(console, "Best use another key or sort the file first")
call GetYesNo "Continue anyway y/n?", "n"
if result = "N" then
call ErrorProc "Request cancelled"
end
return
/*
Ask for a Y or N response. Nothing else will be accepted
*/
GetYesNo:
parse arg msg, default
err = "Y"
do while err = "Y"
call writeln(console, msg "["default"]")
ans = upper(readln(console))
if ans = "" then
ans = upper(default)
if ans = "Y" | ans = "N" then
err = "N"
else
call writeln(console, "Answer y or n")
end
return ans
/*
Display error message and exit
------------------------------
*/
ErrorProc:
arg errmsg
call writeln(console, errmsg)
call writeln(console, "Press RETURN to continue")
ans = readln(console)
exit(10)