Search and replace math operations with the result in Notepad++

notepad

I have several large txt files the following examples:

data="32/3"
count id="3" data="0.0237/4"
ext min="1" max="3" data="28.69*2"

My goal is to search for all the math operations and replace them with its results, like this:

data="10.666667"
count id="3" data="0.005925"
ext min="1" max="3" data="57.38"

Is there any way to automate this in Notepad++?

Best Answer

You can automate that in Notepad++ by creating shortcut to external VBScript. Here is the script:

Option Explicit

Const FileEncoding = 0 ' 0 = ASCII, -1 = Unicode, -2 = System Default
Const FractDigits = 6 ' number of fractional digits

Dim objList, strPath

If WScript.Arguments.Count = 0 then
    CreateObject("WScript.Shell").PopUp "Drop folder(s) and / or file(s) to the script to process", 3, , 48
    WScript.Quit
End If

Set objList = ReadContent(WScript.Arguments)

If objList.Count = 0 Then
    CreateObject("WScript.Shell").PopUp "No files found", 3, , 48
    WScript.Quit
End If

With CreateObject("VBScript.RegExp")
    .Global = True
    .MultiLine = True
    .IgnoreCase = False
    .Pattern = "(\w+=)""([\.\d\(\)\\\*\+/-]*)"""
    For Each strPath In objList
        WriteToFile .Replace(objList(strPath), GetRef("FnReplace")), strPath, FileEncoding
    Next
End With
CreateObject("WScript.Shell").PopUp "Completed", 1, , 64

Function FnReplace(strMatch, strSubMatch1, strSubMatch2, lngPos, strSource)
    Dim strResult
    On Error Resume Next
    strResult = CStr(Round(Eval(strSubMatch2), FractDigits))
    If Err Then
        Err.Clear
        FnReplace = strMatch
    Else
        FnReplace = strSubMatch1 & """" & strResult & """"
    End If
End Function

Function ReadContent(arrList)
    Dim objList, strPath
    Set objList = CreateObject("Scripting.Dictionary")
    For Each strPath In arrList
        AddContent strPath, objList
    Next
    Set ReadContent = objList
End Function

Sub AddContent(strPath, objList)
    Dim objItem
    With CreateObject("Scripting.FileSystemObject")
        If .FileExists(strPath) Then
            objList(strPath) = ReadFromFile(strPath, FileEncoding)
        End If
        If .FolderExists(strPath) Then
            For Each objItem In .GetFolder(strPath).Files
                AddContent objItem.Path, objList
            Next
            For Each objItem In .GetFolder(strPath).SubFolders
                AddContent objItem.Path, objList
            Next
        End If
    End With
End Sub

Function ReadFromFile(strPath, intFormat)
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 1, False, intFormat)
        ReadFromFile = ""
        If Not .AtEndOfStream Then ReadFromFile = .ReadAll
        .Close
    End With
End Function

Sub WriteToFile(strCont, strPath, intFormat)
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 2, True, intFormat)
        .Write(strCont)
        .Close
    End With
End Sub

Do the following:

  • Save this script to file, eg C:\Test\MathResults.vbs
  • Open your text file in Notepad++
  • Click Menu - Run (or F5)
  • Enter "C:\Test\MathResults.vbs" "$(FULL_CURRENT_PATH)" including quotes into The Program to Run field
    specify the path
  • Click Save...
  • Create shortcut, entering eg MathResults as name and Ctrl + F7 as hot keys
    choose the hot keys
  • Click OK
  • Click Run

Now your shortcut has been saved in configuration file, you can just open text files, press Ctrl + F7, once the script completes processing, reload dialog appears, click Yes to display changed file (you may setup the file to be reloaded automatically after been changed). That's it.

BTW, this script works standalone perfectly, you can select in explorer window or on desktop number of files and folders to be processed, and then drop it to the script file.

Related Question