SilverAge Software

Search and Replace. Edit. Transform.

Use Cases

HOWTO: Use Scenarios to Automate Work

We introduced scenarios in the HandyFile Find And Replace so that you can get rid of the monotonous operations that involve searching and replacing many different portions of text in multiple files. Let us consider an example of cleaning HTML files saved from within Microsoft Word.

We will want to:

There are much more Word dirt in the HTML but we shall limit our example to these searches.

We start by opening the Scenario editor by clicking the Advanced Search button and selecting Manage Scenarios from the menu.

In the Scenario dialog, create a new scenario by clicking the New button. Type the name of the new scenario, for example, Clean up Word HTML.

After the new scenario is created, start adding actions to it.

  1. Click the button Add Action and give it a name for the first action, for example: Remove comments.
  2. We shall need regular expressions to cover all comments in a file.

    So we check the Regular expressions option and type the following text in the Find what field:

    \<\!\-\-.#\-\-\>

    Here:

    • \<\!\-\- is the comment opening tag;
    • .# (any character + match 1or more times non-greedy) means "match all further text but stop before the next symbol in the expression (non-greedy)";
    • \-\-\> is the comment closing tag.

    Please note that you should escape all non-alphanumeric symbols when searching with regular expressions.

    As we want to remove comments, we leave the Replace with field blank.

  3. To delete SPAN tags, we create a new action and also use regular expressions but more complex for the Find what field:

    \<span\s#lang\=\w#\-\w#\s#style\=\'mso.#\'\>(.#)\<\/span\>

    Here:

    • #lang\=\w#\-\w# stands for the LANG attribute with the language abbreviation;
    • style\=\'mso.#\' matches any style attribute starting from any mso style attribute;
    • (.#) is the tag inner text that we must preserve.

    We replace the found text with the tag inner text:

    \1

    For the sake of brevity, we shall omit the description of "monkey-see-monkey-do" button clicks.

  4. To delete tags <o:p>, we do not need regular expressions as they always have only one form:

    Find what: <o:p></o:p>

    Replace with: empty

  5. To clean the HTML tag, we need regular expressions again:

    Find what: \<HTML.#\>

    Replace with: <HTML>

    Escapes are generally optional in the replace field.

  6. And finally, we shall remove the Office [if] conditions.

    Find what: \<\!\[if\s.#\]\>(.#)\<\!\[endif\]\>

    Replace with: \1

    Notice: \1 is a replacement expression. To insert \1 literally, you would have to type: \\1.

We have now created a new scenario. Click the Save and Activate button. The Files and Text Tab will change to Advanced mode showing the scenario name.

Now do the final preparations:

Note
Don't forget you can run HandyFile (and Text Workbench) from the command line specifying the name of a scenario by using the parameter: /scn:"scenario_name".