vngreez@vk |
18-03-2017 20:23 2720820 |
çàìåíà ïîäñòðîê èç îäíîãî ôàéëà â äðóãîé
Âëîæåíèé: 1
Èìåþòñÿ 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
.....
|
Ñêðûòûé òåêñò
Êîä:
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 2720906 |
Iska, À ìîæíî ñäåëàòü, ÷òîáû àáñîëþòíûå ïóòè íå èñïîëüçîâàëèñü? (÷òîáû ñêðèïò ðàáîòàë èç ëþáîé ïàïêè)
Íàïðèìåð êàê â áàòíèêå:
strSourceFile = "%dp0\0053\build.prop"
strPatternFile = "%dp0\0053\path.txt"
strDestFile = "%dp0\0053\new_build.prop"
Âîçìîæíî ñäåëàòü ÷òî-òî ïîäîáíîå?
|
Ìîæíî. Òîëüêî òî, ÷òî Âû ïðèâîäèòå â ïðèìåð — ýòî âîâñå íå «èç ëþáîé ïàïêè», à ñòðîãî «èç òîãî æå êàòàëîãà, â êîòîðîì ðàñïîëîæåí ïàêåòíûé ôàéë». Áóäåì ñ÷èòàòü, ÷òî íà ñàìîì äåëå Âàñ èíòåðåñóåò èìåííî âòîðîå (õîòü ýòî è íå åñòü ïðàâèëüíî).
Ñêðûòûé òåêñò
Êîä:
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
|
Âðåìÿ: 22:40.
© OSzone.net 2001-