SilverAge Software

Search and Replace. Edit. Transform.

Use Cases

HOWTO: How to insert a reference to a file with respect to directory hierarchy

The Problem

You have to add a reference to a CSS file style.css which is in the folder C:\MyWeb\HTML to all your HTML pages in different folders starting from C:\MyWeb\HTML.

The Solution

  1. Specify the folder in which you want to find files. For example: C:\MyWeb\HTML
  2. Set mask(s) of the files that you want to find. For example: *.htm*
  3. We're going to insert the LINK tag right after the HEAD tag. So, we shall provide the following pattern in the Find What field:
    \<HEAD\>
  4. Provide the replacement expression:
    \0\r\n<link rel=stylesheet 
           href=\"\ph:"c:\\myweb\\html\\style.css"\" type="text/css">

    here:

    • \0 is the found text (<HEAD>);
    • \ph:"c:\\myweb\\html\\style.css" executes the path resolve operator in HTML mode (note the h letter) and inserts a relative path to c:\myweb\html\style.css.
  5. Make sure the option Force search is unchecked.
  6. Click the Replace button.

The Result

This will insert the following text in all HTML files in, say, C:\MyWeb\HTML\Catalog:

<link rel=stylesheet href="../style.css" type="text/css">

and the following text in HTML files in C:\MyWeb\HTML\Catalog\dir1\dir2:

<link rel=stylesheet href="../../../style.css" type="text/css">

Note!
The root portions of path names of both files (the one a link to which is created and another - to which a link is inserted) must be the same. It means that they must have a common drive letter.