name Replace group "&Learning"; filename = ""; connection = ""; file = false; sub getFile () returnValue = False; result = dialog("What file to search?",2,""); if result.button = 1 filename = result.enteredtext; result = dialog("Host Connection for file, if not local?",2,""); if result.button = 1 connection = result.enteredtext; returnValue = True; endif endif return returnValue; endsub result = dialog("Regular Expression String to Find and Replace?",2,""); if result.button = 2 stop; endif target = result.enteredtext; if (target = "") stop; endif; result = dialog("Replacement string?",2,""); if result.button = 2 stop; endif replacement = result.enteredtext; if (replacement = "") stop; endif; repeat while (typeof(file)<>qedit.typeobject) if not getFile() stop; endif; -- writelog(string(connection) + string(filename)); try file = open(connection: connection, filename: filename); recover result = dialog("Unable to open that file. Try again."); endtry endrepeat file.activate(); -- bring the file to the front file.select(line:1, column:1); -- may be at previous location after Open() if (file.find(regexp: target)) goFlag=true; else goFlag=false; result=dialog("No matches in this file"); endif repeat while (goFlag <> False) result=dialog("Replace this one with " + replacement + "?",2); if result.button=1 -- Do this update result=file.find(regexp: target, selectiononly:true, replacewith: replacement); if not (result) result=dialog("Error doing replacement!"); goFlag = false; endif endif if (goFlag) and not (file.find(regexp: target)) goFlag = false; result=dialog("End of file."); endif endrepeat; file.select(line: 1, column: 1); -- cancel last selection file.close(forceoverwrite:true); << Author: bgreen@robelle.com Date: Sept 30, 1999 Name: Replace Group: Learning Notes: 1. Notice that variables can have the same name as parameter names (i.e., filename, connection and regexp). 2. After a successful Find(), the next search starts after the current match. In this way you do not keep matching the same string over and over. 3. This script uses a Try-Recover block to handle file open errors. 4. Sometimes the dialog box that asks if you want to perform the next string update will actually cover the selected string in the text. Just drag the dialog box away by the title bar before clicking Okay or Cancel. >>