omegastripes

tmp_MathResults_le_for_vutrivi99.vbs

Jun 28th, 2018
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' http://superuser.com/questions/968315/search-and-replace-math-operations-with-the-result-in-notepad
  2.  
  3. Option Explicit
  4.  
  5. Const FileEncoding = 0 ' 0 = ASCII, -1 = Unicode, -2 = System Default
  6. Const FractDigits = 1 ' number of fractional digits
  7.  
  8. Dim objList, strPath
  9.  
  10. If WScript.Arguments.Count = 0 then
  11.     CreateObject("WScript.Shell").PopUp "Drop folder(s) and / or file(s) to the script to process", 3, , 48
  12.     WScript.Quit
  13. End If
  14.  
  15. Set objList = ReadContent(WScript.Arguments)
  16.  
  17. If objList.Count = 0 Then
  18.     CreateObject("WScript.Shell").PopUp "No files found", 3, , 48
  19.     WScript.Quit
  20. End If
  21.  
  22. With CreateObject("VBScript.RegExp")
  23.     .Global = True
  24.     .MultiLine = True
  25.     .IgnoreCase = False
  26.     .Pattern = "(,)([\.\d\(\)\\\*\+/-]*)"
  27.     For Each strPath In objList
  28.         WriteToFile .Replace(objList(strPath), GetRef("FnReplace")), strPath, FileEncoding
  29.     Next
  30. End With
  31. CreateObject("WScript.Shell").PopUp "Completed", 1, , 64
  32.  
  33. Function FnReplace(strMatch, strSubMatch1, strSubMatch2, lngPos, strSource)
  34.     Dim strResult
  35.     On Error Resume Next
  36.     strResult = CStr(Round(Eval(strSubMatch2), FractDigits))
  37.     If Err Then
  38.         Err.Clear
  39.         FnReplace = strMatch
  40.     Else
  41.         FnReplace = strSubMatch1 & strResult
  42.     End If
  43. End Function
  44.  
  45. Function ReadContent(arrList)
  46.     Dim objList, strPath
  47.     Set objList = CreateObject("Scripting.Dictionary")
  48.     For Each strPath In arrList
  49.         AddContent strPath, objList
  50.     Next
  51.     Set ReadContent = objList
  52. End Function
  53.  
  54. Sub AddContent(strPath, objList)
  55.     Dim objItem
  56.     With CreateObject("Scripting.FileSystemObject")
  57.         If .FileExists(strPath) Then
  58.             objList(strPath) = ReadFromFile(strPath, FileEncoding)
  59.         End If
  60.         If .FolderExists(strPath) Then
  61.             For Each objItem In .GetFolder(strPath).Files
  62.                 AddContent objItem.Path
  63.             Next
  64.             For Each objItem In .GetFolder(strPath).SubFolders
  65.                 AddContent objItem.Path
  66.             Next
  67.         End If
  68.     End With
  69. End Sub
  70.  
  71. Function ReadFromFile(strPath, intFormat)
  72.     With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 1, False, intFormat)
  73.         ReadFromFile = ""
  74.         If Not .AtEndOfStream Then ReadFromFile = .ReadAll
  75.         .Close
  76.     End With
  77. End Function
  78.  
  79. Sub WriteToFile(strCont, strPath, intFormat)
  80.     With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 2, True, intFormat)
  81.         .Write(strCont)
  82.         .Close
  83.     End With
  84. End Sub
Advertisement
Add Comment
Please, Sign In to add comment