Войти

Показать полную графическую версию : [решено] замена подстрок из одного файла в другой


vngreez@vk
18-03-2017, 20:23
Имеются 2 файла:
build.prop - файл к которому применяется патч
path.txt - файл патч для build.prop
new_build.prop - build.prop с замененными строками
=================================
файл build.prop состоит так:
# Комментарий
Переменная1=значение
Переменная2=значение
Переменная3=значение
...

# Комментарий
ПеременнаяN=значение
....
=================================
В файле path.txt такой же,но без комментариев
=================================
Необходимо Значений найденных переменных заменить из файла path.txt в build.prop, и получившийся файл переименовать в new_build.prop
=================================
Например:

build.prop
....
name=rea
brand=apple
model=no1
....
path.txt

name=cool
model=abc

new_build.prop
....
name=cool
brand=apple
model=abc
.....

Iska
19-03-2017, 07:19
Option Explicit

Dim strSourceFile
Dim strPatternFile
Dim strDestFile

Dim strContent
Dim strLine
Dim arrPattern

Dim objDictionary
Dim strKey


strSourceFile = "C:\Мои проекты\0053\build.prop"
strPatternFile = "C:\Мои проекты\0053\path.txt"
strDestFile = "C:\Мои проекты\0053\new_build.prop"



With WScript.CreateObject("Scripting.FileSystemObject")
If .FileExists(strSourceFile) Then
If .FileExists(strPatternFile) Then
With .OpenTextFile(strPatternFile)
strContent = .ReadAll()
.Close
End With

Set objDictionary = WScript.CreateObject("Scripting.Dictionary")

For Each strLine In Split(strContent, vbLf)
strLine = Trim(strLine)

If Len(strLine) > 0 Then
arrPattern = Split(strLine, "=")
objDictionary.Add Trim(arrPattern(0)), Trim(arrPattern(1))
End If
Next

With .OpenTextFile(strSourceFile)
strContent = .ReadAll()
.Close
End With

With WScript.CreateObject("VBScript.RegExp")
.IgnoreCase = True
.Global = True

For Each strKey In objDictionary.Keys
.Pattern = "[\f\t\v]*(" & strKey & ")[\f\t\v]*=[\f\t\v]*?.*[\f\t\v]*"

If .Test(strContent) Then
strContent = .Replace(strContent, "$1=" & objDictionary.Item(strKey))
End If
Next
End With

With .CreateTextFile(strDestFile, True)
.Write strContent
.Close
End With

objDictionary.RemoveAll
Set objDictionary = Nothing
Else
WScript.Echo "Can't find pattern file [" & strPatternFile & "]."
WScript.Quit 2
End If
Else
WScript.Echo "Can't find source file [" & strSourceFile & "]."
WScript.Quit 1
End If
End With

WScript.Quit 0

Вообще-то, патч по-англицки именуется «patch».

vngreez@vk
19-03-2017, 08:32
Iska, А можно сделать, чтобы абсолютные пути не использовались? (чтобы скрипт работал из любой папки)
Например как в батнике:
strSourceFile = "%dp0\0053\build.prop"
strPatternFile = "%dp0\0053\path.txt"
strDestFile = "%dp0\0053\new_build.prop"
Возможно сделать что-то подобное?

Iska
19-03-2017, 10:48
Можно. Только то, что Вы приводите в пример — это вовсе не «из любой папки», а строго «из того же каталога, в котором расположен пакетный файл». Будем считать, что на самом деле Вас интересует именно второе (хоть это и не есть правильно).

Option Explicit

Dim strSourceFile
Dim strPatternFile
Dim strDestFile

Dim strPath2Script

Dim strContent
Dim strLine
Dim arrPattern

Dim objDictionary
Dim strKey


strSourceFile = "build.prop"
strPatternFile = "path.txt"
strDestFile = "new_build.prop"

With WScript.CreateObject("Scripting.FileSystemObject")
strPath2Script = .GetParentFolderName(WScript.ScriptFullName)

strSourceFile = .BuildPath(strPath2Script, strSourceFile)
strPatternFile = .BuildPath(strPath2Script, strPatternFile)
strDestFile = .BuildPath(strPath2Script, strDestFile)

If .FileExists(strSourceFile) Then
If .FileExists(strPatternFile) Then
With .OpenTextFile(strPatternFile)
strContent = .ReadAll()
.Close
End With

Set objDictionary = WScript.CreateObject("Scripting.Dictionary")

For Each strLine In Split(strContent, vbLf)
strLine = Trim(strLine)

If Len(strLine) > 0 Then
arrPattern = Split(strLine, "=")
objDictionary.Add Trim(arrPattern(0)), Trim(arrPattern(1))
End If
Next

With .OpenTextFile(strSourceFile)
strContent = .ReadAll()
.Close
End With

With WScript.CreateObject("VBScript.RegExp")
.IgnoreCase = True
.Global = True

For Each strKey In objDictionary.Keys
.Pattern = "[\f\t\v]*(" & strKey & ")[\f\t\v]*=[\f\t\v]*?.*[\f\t\v]*"

If .Test(strContent) Then
strContent = .Replace(strContent, "$1=" & objDictionary.Item(strKey))
End If
Next
End With

With .CreateTextFile(strDestFile, True)
.Write strContent
.Close
End With

objDictionary.RemoveAll
Set objDictionary = Nothing
Else
WScript.Echo "Can't find pattern file [" & strPatternFile & "]."
WScript.Quit 2
End If
Else
WScript.Echo "Can't find source file [" & strSourceFile & "]."
WScript.Quit 1
End If
End With

WScript.Quit 0




© OSzone.net 2001-2012