Tapatalk

How to do multiple finds and replaces in active document with an UltraEdit script?

How to do multiple finds and replaces in active document with an UltraEdit script?

1
NewbieNewbie
1

    May 15, 2023#1

    Hello, I need a script to replace in the active document many strings and each string has a different replacement string.

    For example:

    Find: "wbrp01.i" replace: "{us/wb/wbrp01.i}"
    Find: "{gprun.i " replace: "{us/bbi/gprun.i"
    Find: "{mfdtitle.i}" replace: "{us/mf/mfdtitle.i}"

    6,685587
    Grand MasterGrand Master
    6,685587

      May 16, 2023#2

      Here is the simple script for the task. Please read the comments.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define the two arrays with the strings to find and the appropriate replace strings.
         // Please note that the character \ in a find/replace string must be escaped with one
         // more \ as the backslash is the escape character in a JavaScript string.
         var asFind = ["wbrp01.i", "{gprun.i ", "{mfdtitle.i}"];
         var asReplace = ["{us/wb/wbrp01.i}", "{us/bbi/gprun.i", "{us/mf/mfdtitle.i}"];
      
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         // Move caret to top of the active file.
         UltraEdit.activeDocument.top();
      
         // Define all the parameters for the case-sensitive, non regular
         // expression replace all commands executed on entire active file.
         UltraEdit.ueReOn();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=false;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
         {
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
         }
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
      
         // The number of strings to find and the number of replace
         // strings should be always identical. But make nevertheless
         // sure that there is never an array access out of bounce.
         var nReplaceCount = asFind.length;
         if (asReplace.length < nReplaceCount) nReplaceCount = asReplace.length;
      
         for (var nIndex = 0; nIndex < nReplaceCount; ++nIndex)
         {
            UltraEdit.activeDocument.findReplace.replace(asFind[nIndex], asReplace[nIndex]);
            UltraEdit.activeDocument.top();  // This command should not be necessary, but
         }                                   // there are some UE/UES versions requiring it.
      }
      
      Best regards from an UC/UE/UES for Windows user from Austria