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
Zergal



PostPosted: Tue May 16, 2006 19:49    Post subject: Reply with quote

Вот это мне и было нужно, спасибо.
Я чуть поменял пример из Борна, добавил Батин скрипт, взболтал но не перемешивал и получил то же но быстрее и без всяких emeditor.exe за которых между прочем бабки просят Wink
Code:

'**************************************************
' 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



ЗЫ Может сбоить на символах, которые не понимает шаблон.
Не причёсывал, лень лохматая.
Ругайтесь, мне будет приятно.


Last edited by Zergal on Wed Nov 25, 2009 09:51; edited 1 time in total
View user's profile Send private message Send e-mail Visit poster's website


Powered by phpBB © 2001, 2005 phpBB Group