'**************************************************
' File: ReplaceTxt.vbs (WSH sample in VBScript)
' Author: (c) G. Born, Batya, Zergal
'
' Что-то меняет в Ваших файлах
' УРА - Никакого бэкапа!!!
'**************************************************
Option Explicit
Dim WS, FSO2, StreamFile, Find_String, Replace_String, Current_File, MsgBut
Set WS = WScript.CreateObject("WScript.Shell")
Set FSO2 = CreateObject("Scripting.FileSystemObject")
Set StreamFile = FSO2.OpenTextFile(WScript.Arguments(0), 1)
Find_String = InputBox("Введите искомую строку", "Поиск и замена")
if Len(Find_String) = 0 then
MsgBut = MsgBox("Не задана строка для поиска!", vbOKOnly + vbExclamation, "Поиск и замена")
WScript.Quit()
End if
Replace_String = InputBox("Введите строку для замены", "Поиск и замена")
' We must define ForWriting because the
' iomode constants are unknown to VBScript.
Const ForWriting = 2 ' iomode: write access
' Const inF = "TestFile.txt"
' Const outF = "TestFile1.txt"
Do While Not StreamFile.AtEndOfStream
' Here are the strings for replacement.
Dim Text
Dim fso, oFile ' Object variable
Dim oFileIn, oFileOut ' Text stream
Dim path, fileIn, fileOut
path = StreamFile.ReadLine ' Retrieve current path to script.
fileIn = path ' Create filenames.
fileOut = left(path, len(path)-4) & "_" & right(path,4)
' Create a FileSystemObject object to access the file system.
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(fileIn) Then ' Input file exists?
WScript.Echo "File '" & fileIn & "' not found"
WScript.Quit 1
End If
' Input file present; open file and create output file.
Set oFileIn = fso.OpenTextFile(fileIn) ' Open input file.
Set oFileOut = fso.OpenTextFile(fileOut, _
ForWriting, True) ' Open output file.
Do While Not (oFileIn.atEndOfStream)
Text = oFileIn.ReadLine ' Read a line.
Text = Filter(Text, Find_String, Replace_String)
oFileOut.WriteLine Text ' Write text.
Loop
oFileOut.Close
oFileIn.Close
fso.DeleteFile FileIn
Set oFile = fso.GetFile(fileOut)
oFile.move FileIn
Set oFileOut = nothing
Set oFileIn = nothing
Loop
Function GetPath
' Retrieve the script path.
Dim path
path = WScript.ScriptFullName ' Script name
GetPath = Left(path, InStrRev(path, "\"))
End Function
Function Filter(txt, expr1, expr2)
' Replace expr1 with expr2 in text.
Dim oReg
Set oReg = New RegExp ' Create regular expression.
oReg.Global = True ' All matches
oReg.IgnoreCase = True ' Make case-insensitive.
' Replace all expr1 with expr2.
oReg.Pattern = expr1 ' Set pattern.
Filter = oReg.Replace(txt, expr2)
End Function
'*** End
|