User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Help with writing and running scripts
6 posts Page 1 of 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 "blablabla" found (in any of the files), replace with term "blebleble". 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.
Sebastian wrote: If not found continue to the next term.

This requirement is tricky.

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

I have written in the past already several UltraEdit macros and scripts to run multiple replaces on one file using Replace command or all files in a folder using Replace in Files command 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 Austria
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
Find
<img src="http://i.imgur.com/WGSdFFM.jpg">
Replace with
<img src="http://111.imagebam.com/download/T4mrbaO2rbpd5J6AoHdY3Q/39909/399083018/WGSdFFM.jpg">


Find
<img src="http://i.imgur.com/R8zIzQC.jpg">
Replace with
<img src="http://112.imagebam.com/download/g2hKpSfrCfBJsYURTR4Y2w/39909/399082906/R8zIzQC.jpg">
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();
   UltraEdit.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;

      // 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 Austria
Thanks a million Mofi

It worked perfectly.
I do such stuff with VBScript and the VBScript RegExpr (Regular Expression) object. I have written a script which goes through all files (HTM files in that case) in a given directory (using FSO, i.e. the File System Object) and applies a list of search/replace expressions from a file and outputs the modified file to another directory. I think that VBScript is the better tool than an text editor for this kind of task.
6 posts Page 1 of 1