Replace Text In Notebook

// ReplaceTextInNotebook(nb, textToFind, newText, findFlags)
// Replaces text in the specified notebook.
// textToFind must not contain carriage returns.
// newText can contain carriage returns.
// See Notebook findText documentation for values for findFlags.
// NOTE:    This does the replacement in the whole notebook.
//          You may need to tweak it for your purposes.
// Example:
//  Print ReplaceTextInNotebook("Notebook0", ";", "\r", 0)
Function ReplaceTextInNotebook(nb, textToFind, newText, findFlags)
    String nb           // Name of notebook or "" for top notebook
    String textToFind   // This can not span paragraphs - i.e., must not contain CRs
    String newText      // This can include CRs
    Variable findFlags  // See Notebook findText documentation
   
    if (strlen(nb) == 0)
        nb = WinName(0, 16, 1)
    endif
    if (strlen(nb) == 0)
        Abort "There are no notebooks"      // Actually there are no *visible* notebooks
    endif
   
    Notebook $nb, selection={StartOfFile, StartOfFile}
   
    Variable numReplacements = 0
    do
        Notebook $nb, findText={textToFind, findFlags}
        if (V_flag == 0)
            break               // Text not found
        endif
        Notebook $nb, text=newText
        numReplacements += 1
    while(1)   
   
    return numReplacements
End

Forum

Support

Gallery

Igor Pro 8

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More