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.

Code: Select all
<img src="">
Replace with
<img src="">

<img src="">
Replace with
<img src="">
Here is the script for this task:

Code: Select all
if (UltraEdit.document.length > 0)  // Is any file opened?
   // Define environment for this script.

   // Select entire contents of active file with DOS line terminators.
   // 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();;  // 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.

      // 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]);

It was tested with Test1.html and Test2.htm in directory C:\Temp\Test with active file on script execution containing

Code: Select all

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