Tapatalk

Find double text strings in a file and add a number to each

Find double text strings in a file and add a number to each

2
NewbieNewbie
2

    Aug 25, 2022#1

    Hello, I try to repair a DXF file (drawing exchange format, CAD). The problem with this text file is, that because of the doubled names inside the file the import into the CAD program does not work. So I want to try the following:
    If the text string "Building" is  found the macro/script should rename the first string to "Building_01", the next to "Building_02" and so on. Is this possible with UE?

    6,685587
    Grand MasterGrand Master
    6,685587

      Aug 26, 2022#2

      Here is a very quick coded UltraEdit script for this task. There must be opened first the DXF file to modify. Next create a new file, copy and paste the script code below into the new file, save it with file name AddBuildingNumber.js and click on ribbon/menu Advanced on the command Play script on using ribbon mode or toolbar/menu mode with contemporary menus respectively in menu Scripting on menu item Run active script on using the toolbar/menu mode with traditional menus.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.document[0].columnModeOff) == "function") UltraEdit.document[0].columnModeOff();
      
         // Move caret to top of the active file.
         UltraEdit.document[0].top();
      
         // Define the parameters for a non-regular expression replace in a loop.
         UltraEdit.ueReOn();
         UltraEdit.document[0].findReplace.mode=0;
         UltraEdit.document[0].findReplace.matchCase=true;
         UltraEdit.document[0].findReplace.matchWord=true;
         UltraEdit.document[0].findReplace.regExp=false;
         UltraEdit.document[0].findReplace.searchDown=true;
         if (typeof(UltraEdit.document[0].findReplace.searchInColumn) == "boolean")
         {
            UltraEdit.document[0].findReplace.searchInColumn=false;
         }
         UltraEdit.document[0].findReplace.preserveCase=false;
         UltraEdit.document[0].findReplace.replaceAll=false;
         UltraEdit.document[0].findReplace.replaceInAllOpen=false;
      
         // Search case-sensitive with matching whole words only for "Building"
         // and replace each found occurrence using a loop with the current
         // replace string being "Building_" with an incremented number
         // appended with at least two digits.
         var sReplaceText;
         var nNumber=0;
         do
         {
            nNumber++;
            sReplaceText = (nNumber >= 10) ? "Building_" : "Building_0";
            sReplaceText += nNumber.toString(10);
         }
         while (UltraEdit.document[0].findReplace.replace("Building",sReplaceText));
      }
      
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Aug 26, 2022#3

        Thank you very much for your help. It works like intended. Very good!
        Have a nice weekend!