gpt4 book ai didi

regex - 使用 VBS 替换两个文件之间的变量值

转载 作者:行者123 更新时间:2023-12-04 06:17:37 24 4
gpt4 key购买 nike

我正在处理一个脚本,没有你的帮助我将无法创建它。
这就是我需要的:我有两个 .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

最佳答案

测试了这种蛮力方法,它对我有用......

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@=

关于regex - 使用 VBS 替换两个文件之间的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7027746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com