Processing Replace in Files for all strings listed line by line in an opened file

Processing Replace in Files for all strings listed line by line in an opened file

8
NewbieNewbie
8

    Nov 19, 2013#1

    Hi,
    I have many files across many folders that contain a number of strings that need tags around them.
    The strings for tagging are stored in a separate file as a list with each string on a separate line.
    What I want to do is read each line from the file and carry out the find/replace on a folder.

    I know how to do most of this but I'm having problems reading each line of the find/replace file and determining how I have reached the end of the file.
    A Google search turned up many examples of JavaScript that carry out this functionality but when I put them into an UnltraEdit script, they don't work.

    Any suggestions?

    Thanks

    6,604548
    Grand MasterGrand Master
    6,604548

      Nov 19, 2013#2

      Here is a template for doing something with strings stored line by line in a list file being the active file on starting the script.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         UltraEdit.columnModeOff();
         // Select everything in active file.
         UltraEdit.activeDocument.selectAll();
         // Is active file not an empty file?
         if (UltraEdit.activeDocument.isSel())
         {
            // Determine line ending type of active file (just UNIX or DOS).
            var sLineEding = (UltraEdit.activeDocument.lineTerminator == 1) ? "\n" : "\r\n";
            // Get each line of the file into an array of strings with
            // each line being a string without line ending characters.
            var asLines = UltraEdit.activeDocument.selection.split(sLineEding);
            // If the last string is an empty string because of file ends with
            // a line termination, remove this empty string from the array.
            if (asLines[asLines.length-1] == "") asLines.pop();
      
            // Do something with each string (line) in the array.
            for (var nLine = 0; nLine < asLines.length; nLine++)
            {
               // Remove the next line if empty lines do not exist in the list file.
               if (!asLines[nLine].length) continue; // Skip empty strings (lines).
               // ... do whatever you want to do with each string referenced by
               //     asLines[nLine] in the array of strings with name asLines.
            }
         }
      }
      Best regards from an UC/UE/UES for Windows user from Austria

      8
      NewbieNewbie
      8

        Nov 19, 2013#3

        Thanks, that's great!

        Now that I have that working, it has brought another problem to light:
        I need to use regex for the find/replace because I need to capture the strings and place tags around them. However, some of the strings have characters in them that need to be escaped.
        Is there an easy way to escape a full string (essentially ignoring regex just for that string) or do I have to process each string and escape each required character before carrying out the find/replace?

        Thanks

        6,604548
        Grand MasterGrand Master
        6,604548

          Nov 19, 2013#4

          Code: Select all

          var sEscapedString = asLines[nLine].replace(/([^\w<>])/g,"\\$1");
          This little line inserts a backslash before every non alphanumeric character except < and > in the actual string from the list file. Use this in the loop before creating the entire search string. It does not matter that with this replace also some characters are escaped which have no special meaning in a regular expression search string.
          Best regards from an UC/UE/UES for Windows user from Austria

          8
          NewbieNewbie
          8

            Nov 21, 2013#5

            Awesome!

            Thanks :)