Total Commander Forum Index Total Commander
Форум поддержки пользователей Total Commander
Сайты: Все о Total Commander | Totalcmd.net | Ghisler.com | RU.TCKB
 
 RulesRules   SearchSearch   FAQFAQ   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Single Post  Topic: Windows Script Host 
Author Message
Volniy



PostPosted: Tue May 16, 2006 22:28    Post subject: 4-ая редакция Reply with quote

Почти то же самое, но только по-человечески Wink
Code:
'=========================[ Start of file ]=========================
'  File:         ReplaceInFiles.vbs
'  Description:  Производит поиск текста с заменой
'                в выделенных текстовых файлах     
'  Installation: Создайте кнопку на панели TC
'                Команда: "диск:\путь\к\файлу\ReplaceInFiles.vbs"
'                Параметры: "%L"
'  Copyright:    (c) 2006-2008, Volniy

Option Explicit

Dim FSO, Find_String, Replace_String, F, Buffer, ListFile, Found
Const ForReading = 1, ForWriting = 2

Set FSO = CreateObject("Scripting.FileSystemObject")

Find_String = InputBox("Введите искомую строку", "Поиск и замена")
If Len(Find_String) = 0 Then
   MsgBox "Не задана строка поиска!", vbExclamation, "Поиск и замена"
   WScript.Quit()
End If

Replace_String = InputBox("Введите строку для замены", "Поиск и замена")

Set ListFile = FSO.OpenTextFile(WScript.Arguments(0), 1)
Do While Not ListFile.AtEndOfStream
   Call DoItForThisFile(ListFile.ReadLine)
Loop
ListFile.Close
Set ListFile = Nothing
Set F = Nothing
Set FSO = Nothing

MsgBox "Поиск с заменой завершен!", vbInformation, "Поиск и замена"
WScript.Quit

Sub DoItForThisFile(FilePath)
   Set F = FSO.OpenTextFile(FilePath, ForReading)
   Buffer = F.ReadAll
   F.Close
   Found = InStr(1, Buffer, Find_String, 1)
   If Found <> 0 Then
     Buffer = Replace(CStr(Buffer), Find_String, Replace_String,1,-1,1)
     'FSO.CopyFile FilePath, FilePath & ".bak"
     Set F = FSO.OpenTextFile(FilePath, ForWriting)
     F.Write Buffer
     F.Close
   End If
End Sub
'=========================[  End of file  ]=========================


Изменил немного код, чтобы файлы, в которых разыскиваемая строка отсутствует, не перезаписывались (действительно, нах оно?).


Last edited by Volniy on Thu Oct 30, 2008 01:40; edited 8 times in total
View user's profile Send private message


Powered by phpBB © 2001, 2005 phpBB Group