' On Error Resume Next b]Option Explicit Dim [/b]fso, WshShell, SciTE, selected, text, fc, f, Args Dim patrns, first_pattern, second_pattern, find_string, replace_string Set fso = WScript.CreateObject("Scripting.FileSystemObject") Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.CurrentDirectory = fso.GetParentFolderName (WScript.ScriptFullName) Set SciTE = CreateObject("SciTE.Helper") If Err.Number <> 0 Then WScript.Echo "Please install SciTE Helper before!" WScript.Quit 1 End If selected = True text = SciTE.GetSelText If text = "" Then Set Args = WScript.Arguments If Args.Count < 1 Then MsgBox ("Неверные настройки запуска скрипта") WScript.Quit Else If not fso.FileExists (Args(0)) Or fso.FolderExists (Args(0)) Then MsgBox ("Файл не найден - " & Args(0)) WScript.Quit End If End If text = ReadFile (Args(0)) selected = False End If ' Подхват паттернов If fso.FileExists ("recent_search.txt") Then patrns = ReadFile ("recent_search.txt") first_pattern = rex_replace (patrns, "^([^\r\n]*)\r?\n?.*$", "$1") second_pattern = rex_replace (patrns, "^([^\r\n]*)\r?\n?(.*)$", "$2") End If find_string = InputBox("Enter pattern to search", "", first_pattern) If find_string = "" Then WScript.Quit replace_string = InputBox("Enter pattern to replace with", "", second_pattern) ' Передача в консоль количества вхождений WScript.Echo ( vbCRLF & " КОЛИЧЕСТВО ВХОЖДЕНИЙ: " & rex_find (text, find_string) & vbCRLF) text = rex_replace (text, find_string, replace_string) If Selected Then SciTE.ReplaceSel (text) Else ' SciTE.MenuCommand (207) ' SciTE.ReplaceSel (text) WriteFile text, Args(0) WScript.Sleep (30) Scite.MenuCommand (104) End If ' Сохранение паттернов patrns = find_string & vbCRLF & replace_string WriteFile patrns, "recent_search.txt" Function rex_replace(str1,patrn, replStr) Dim regEx Set regEx = New RegExp regEx.Pattern = patrn regEx.IgnoreCase = True regEx.Global = True rex_replace = regEx.Replace(str1, replStr) End Function Function rex_find (strng, patrn) Dim regEx, Match, Matches, i Set regEx = New RegExp regEx.Pattern = patrn regEx.IgnoreCase = True regEx.Global = True Set Matches = regEx.Execute (strng) For Each Match in Matches i = i + 1 Next rex_find = i End Function Sub WriteFile(text, filename) Dim File Const ForReading = 1, ForWriting = 2, ForAppending = 8 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Const CreateFile = True, DoNotCreateFile = False Set fso = CreateObject("Scripting.FileSystemObject") Set File = fso.OpenTextFile(filename, ForWriting, CreateFile) File.Write text File.Close End Sub Function ReadFile(filename) Dim File Const ForReading = 1, ForWriting = 2, ForAppending = 8 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Const CreateFile = True, DoNotCreateFile = False If fso.FileExists(filename) Then If fso.GetFile(filename).Size > 0 Then Set File = fso.OpenTextFile(filename, ForReading, DoNotCreateFile, TristateUseDefault) ReadFile = File.ReadAll File.Close End If End If End Function |