Bulk Find and Replace in several files

Bulk Find and Replace in several files

3
NewbieNewbie
3

    Mar 23, 2015#1

    I would like to find and replace some terms in over 40 HTML files.

    I have a list of the terms to find and a list of the terms to replace for.

    Every file has between 1 and 10 terms to find and replace. The terms are always different.

    How can I do this in bulk?


    Example of what I would like to accomplish:

    If term "String X" found (in any of the files), replace with term "Other String Y". If not found continue to the next term.

    I have a total of 233 terms to find and replace.

    Could someone please help me?

    Thanks in advance.

    6,603547
    Grand MasterGrand Master
    6,603547

      Mar 23, 2015#2

      Sebastian wrote:If not found continue to the next term.
      This requirement is tricky.

      Is it really necessary to evaluate if the replace of first term was successful in a file or any file before replace of second term is processed?

      I have written in the past already several UltraEdit macros and scripts to run multiple replaces on one file using command Replace or on all files in a folder using command Replace in Files from a list file. So this task is nothing new for me. There are already topics containing macro/script code for such replace tasks.

      The format of the data in the list file is something important to know. Is there always a line with a search string and next line contains the replace string? Or are search and replace string on same line with a separator between and what is the separator in this case?
      Best regards from an UC/UE/UES for Windows user from Austria

      3
      NewbieNewbie
      3

        Mar 23, 2015#3

        Thanks for your reply.

        That evaluation is not really necessary.

        I'll give you more info.

        What I want to change is the source of the images in the HTML files.

        Example:

        Code: Select all

        <img src="http://i.imgur.com/WGSdFFM.jpg">
        <img src="http://111.imagebam.com/download/T4mrbaO2rbpd5J6AoHdY3Q/39909/399083018/WGSdFFM.jpg">
        <img src="http://i.imgur.com/R8zIzQC.jpg">
        <img src="http://112.imagebam.com/download/g2hKpSfrCfBJsYURTR4Y2w/39909/399082906/R8zIzQC.jpg">
        The first line contains the first search string to be replaced by the first replace string in second line with first Replace in Files.
        The third line contains the second search string to be replaced by the second replace string in fourth line with the second Replace in Files.

        6,603547
        Grand MasterGrand Master
        6,603547

          Mar 24, 2015#4

          Here is the script for this task:

          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.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
          
             // Select entire contents of active file with DOS line terminators.
             UltraEdit.activeDocument.selectAll();
             // Load the search and replaces strings into an array of strings.
             var asStrings = UltraEdit.activeDocument.selection.split("\r\n");
             // Remove the last string if it is an empty string because
             // of last line of file ended with a line termination.
             if (!asStrings[asStrings.length-1].length) asStrings.pop();
             UltraEdit.activeDocument.top();  // Cancel the selection.
          
             // The number of strings must be even or something is wrong with the list.
             if ((asStrings.length % 2) == 0)
             {
                // Define the parameters for the Replace in Files used to update
                // all *.htm and *.html files in directory C:\Temp\Test and its
                // subdirectories except subdirectories with hidden attribute.
                UltraEdit.frInFiles.filesToSearch=0;
                UltraEdit.frInFiles.searchInFilesTypes="*.htm;*.html";
                UltraEdit.frInFiles.directoryStart="C:\\Temp\\Test\\";
                UltraEdit.frInFiles.searchSubs=true;
                UltraEdit.frInFiles.ignoreHiddenSubs=true;
                UltraEdit.frInFiles.regExp=false;
                UltraEdit.frInFiles.matchCase=true;
                UltraEdit.frInFiles.matchWord=false;
                UltraEdit.frInFiles.logChanges=true;
                UltraEdit.frInFiles.useEncoding=false;
                UltraEdit.frInFiles.preserveCase=false;
                UltraEdit.frInFiles.openMatchingFiles=false;
                UltraEdit.ueReOn();
          
                // Run the Replace in Files with search and replace strings from array.
                for (var nSearch = 0; nSearch < asStrings.length; nSearch += 2)
                {
                   UltraEdit.frInFiles.replace(asStrings[nSearch], asStrings[nSearch+1]);
                }
             }
             UltraEdit.outputWindow.clear();
          }
          
          It was tested with Test1.html and Test2.htm in directory C:\Temp\Test with active file on script execution containing:

          Code: Select all

          http://i.imgur.com/WGSdFFM.jpg
          http://111.imagebam.com/download/T4mrbaO2rbpd5J6AoHdY3Q/39909/399083018/WGSdFFM.jpg
          http://i.imgur.com/R8zIzQC.jpg
          http://112.imagebam.com/download/g2hKpSfrCfBJsYURTR4Y2w/39909/399082906/R8zIzQC.jpg
          
          The script file (ASCII file with DOS line terminators with code above) was before added via Scripting - Scripts to list of scripts and executed on active file via menu Scripting.
          Best regards from an UC/UE/UES for Windows user from Austria

          3
          NewbieNewbie
          3

            Mar 24, 2015#5

            Thanks a million Mofi

            It worked perfectly.