我正在处理一个脚本,如果没有你的帮助,我将无法创建它。
这就是我需要的:我有两个.txt文件,一个包含变量(在两个@
之间),如下所示:
@PickupFolder@=E:/SonicDataFiles/AR_INT/GPP_VE/IN
@Db1Url@=jdbc:sonic:sqlserver://CARASETMS:1433;databaseName=CRM
另一个.txt包含需要从上一个文件
完成的变量@Db1Url@=
@Db1Pwd@=
我需要获取第一个文件中的每个变量(我想我必须使用正则表达式)并在变量退出时替换第二个文件中的值。
我真的很感激,如果有人有类似的脚本来获得一个想法。我正试着用VBS做这件事。
非常感谢。
赫拉尔。
阿根廷布宜诺斯艾利斯。
这是我原来的剧本:
'ReplaceScript "c:\Variables_INT.txt" "C:\AR_INT.tailoring.properties"
'DEFINE CONSTANTS
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
'DEFINE VARIABLES
strTxtFile = Wscript.Arguments(0)
strTailoringFile = Wscript.Arguments(1)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShl = WScript.CreateObject("WScript.Shell")
Set objFileVariablesTXT = objFSO.OpenTextFile (strTxtFile, ForReading) 'Variables_INT.txt
Set objFileTailoring = objFSO.OpenTextFile (strTailoringFile, ForReading) 'AR_INT.tailoring.properties
Dim strQuartz, strPickupfFolder, strUrl, strDbPwd, strDbUser, strDbDestTable
strSearchString = objFileVariablesTXT.ReadAll
'SEARCH THE FILE FOR THE NEEDED DATA
vQuartz = InStr(strSearchString, "@QuartzJars@=")
vPickupFolder = InStr(strSearchString, "@PickupFolder@=")
vDbUrl = InStr(strSearchString, "@Db1Url@=")
vDbPwd = InStr(strSearchString, "@Db1Pwd@=")
vDbUser = InStr(strSearchString, "@Db1User@=")
vDbDestTable = InStr(strSearchString, "@DestinationTable@=")
'PARSE OUT THE NEEDED INFO
If vQuartz <> 0 Then
'vQuartz = vQuartz + 13
strQuartz = Mid(strSearchString, vQuartz, 304)
' WScript.Echo strQuartz
End If
If vPickupFolder <> 0 Then
'vPickupFolder = vPickupFolder + 15
strPickupfFolder = Mid(strSearchString, vPickupFolder, 50)
' WScript.Echo strPickupfFolder
End If
If vDbUrl <> 0 Then
'vDbUrl = vDbUrl + 9
strUrl = Mid(strSearchString, vDbUrl, 65)
' WScript.Echo strUrl
End If
If vDbPwd <> 0 Then
'vDbPwd = vDbPwd + 9
strDbPwd = Mid(strSearchString, vDbPwd, 17)
'WScript.Echo strDbPwd
End If
If vDbUser <> 0 Then
'vDbUser = vDbUser + 10
strDbUser = Mid(strSearchString, vDbUser, 25)
'WScript.Echo strDbUser
End If
If vDbDestTable <> 0 Then
'vDbDestTable = vDbDestTable + 19
strDbDestTable = Mid(strSearchString, vDbDestTable, 29)
'WScript.Echo strDbDestTable
End If
objFileVariablesTXT.Close
strReplaceString = objFileTailoring.ReadAll
arrReplacements = Array("@QuartzJars@=Ç" & strQuartz , "@PickupFolder@=Ç" & strPickupfFolder, "@Db1Url@=Ç" & strUrl, "@Db1Pwd@=Ç" & strDbPwd, "@Db1User@=Ç" &strDbUser, "@DestinationTable@=Ç" & strDbDestTable)
objFileTailoring.Close
Set objFileTailoring = Nothing
For Each strReplacement In arrReplacements
strReplaceWhat = Split(strReplacement, "Ç")(0)
'WScript.Echo strReplaceWhat
strReplaceWith = Split(strReplacement, "Ç")(1)
'WScript.Echo strReplaceWith
strReplaceString = Replace(strReplaceString, strReplaceWhat, strReplaceWith)
Next
'wScript.Echo strReplaceString
Set objFileTailoring = objFSO.OpenTextFile(strTailoringFile, 2, true)
objFileTailoring.Write strReplaceString
objFileTailoring.Close
答案 0 :(得分:2)
快速而肮脏:
Dim sFSpec1 : sFSpec1 = "..\data\frs.txt"
Dim sFSpec2 : sFSpec2 = "..\data\sec.txt"
Dim dicRpl : Set dicRpl = CreateObject( "Scripting.Dictionary" )
Dim reCut : Set reCut = New RegExp
reCut.Global = True
reCut.Pattern = "(@[^@]+@)\s*=\s*(.*?)\s*$"
Dim sAll : sAll = goFS.OpenTextFile( sFSpec1 ).ReadAll
WScript.Echo sAll
WScript.Echo "---------------"
Dim oMTS : Set oMTS = reCut.Execute( sAll )
Dim oMT
For Each oMT In oMTS
dicRpl( oMT.SubMatches( 0 ) ) = oMT.SubMatches( 1 )
Next
sAll = goFS.OpenTextFile( sFSpec2 ).ReadAll
WScript.Echo sAll
WScript.Echo "---------------"
Dim sKey
For Each sKey In dicRpl.Keys
sAll = Replace( sAll, sKey, dicRpl( sKey ) )
Next
WScript.Echo sAll
输出:
@PickupFolder@=E:/SonicDataFiles/AR_INT/GPP_VE/IN
@Db1Url@=jdbc:sonic:sqlserver://CARASETMS:1433;databaseName=CRM
---------------
@Db1Url@=
@Db1Pwd@=
---------------
jdbc:sonic:sqlserver://CARASETMS:1433;databaseName=CRM=
@Db1Pwd@=
如果这原则上解决了您的问题,我们可以确定细节。
增加:
由于Jean-FrançoisCorbett肯定是对的,请使用模式
reCut.Pattern = "(@[^@]+@=)(.*?)\s*$"
和最后的替换
sAll = Replace( sAll, sKey, sKey & dicRpl( sKey ) )
此版本在两个文件中都采用严格的@x@=[y]
格式。
答案 1 :(得分:1)
测试了这种蛮力方法,它对我有用......
Dim FSO, txs, all, sourceLines, i, targetLines, j, delimiterPosition
Set FSO = CreateObject("Scripting.FileSystemObject")
set txs = FSO.OpenTextFile(".\source.txt", 1)
all=txs.ReadAll
txs.Close
sourceLines=Split(all,vbCrLf)
set txs = FSO.OpenTextFile(".\target.txt", 1)
all=txs.ReadAll
txs.Close
targetLines=Split(all,vbCrLf)
for i = 0 to ubound(sourceLines)
If sourceLines(i)<>"" Then
delimiterPosition = InStr(2, sourceLines(i), "@")
sourceVarName = Mid(sourceLines(i), 2, delimiterPosition - 2)
sourceVarValue = Mid(sourceLines(i), delimiterPosition + 2)
for j = 0 to ubound(targetLines)
If targetLines(j)<>"" Then
delimiterPosition = InStr(2, targetLines(j), "@")
targetVarName = Mid(targetLines(j), 2, delimiterPosition - 2)
If targetVarName = sourceVarName Then
targetLines(j) = targetLines(j) & sourceVarValue
End If
End If
next
End If
next
set txs = FSO.OpenTextFile(".\target.txt", 2)
for j = 0 to ubound(targetLines)
txs.WriteLine targetLines(j)
next
txs.Close
目标文件现在是:
@Db1Url@=jdbc:sonic:sqlserver://CARASETMS:1433;databaseName=CRM
@Db1Pwd@=