2011-08-11 98 views
2

我正在處理腳本,如果沒有您的幫助,我將無法創建它。 這就是我需要:我有兩個.txt文件,一個包含變量(二@之間),像這樣:使用VBS替換兩個文件之間的變量值

@[email protected]=E:/SonicDataFiles/AR_INT/GPP_VE/IN   
@[email protected]=jdbc:sonic:sqlserver://CARASETMS:1433;databaseName=CRM 

其他的.txt有需要從以前的文件完成變量

@[email protected]= 
@[email protected]= 

我需要得到第一個文件中的每個變量(我想我必須使用正則表達式),並在第二個文件中替換它的值,如果變量退出。

我真的很感激,如果有人有類似的腳本來得到一個想法。我試圖用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, "@[email protected]=") 
       vPickupFolder = InStr(strSearchString, "@[email protected]=") 
       vDbUrl = InStr(strSearchString, "@[email protected]=") 
       vDbPwd = InStr(strSearchString, "@[email protected]=") 
       vDbUser = InStr(strSearchString, "@[email protected]=") 
       vDbDestTable = InStr(strSearchString, "@[email protected]=") 

      '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("@[email protected]=Ç" & strQuartz , "@[email protected]=Ç" & strPickupfFolder, "@[email protected]=Ç" & strUrl, "@[email protected]=Ç" & strDbPwd, "@[email protected]=Ç" &strDbUser, "@[email protected]=Ç" & 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

感謝讓 - 弗朗索瓦。 是的,我已經創建了一個腳本,但我的問題是,我需要一個腳本來查找文件中的每個變量,我對它們的名字進行了硬編碼以搜索它們 – Gerardo

回答

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 

目標文件現在是:

@[email protected]=jdbc:sonic:sqlserver://CARASETMS:1433;databaseName=CRM 
@[email protected]= 
+0

+1:符合規格比實施更重要。 –

+0

嗨,夥計們,非常感謝您的幫助。對不起,我對vbs沒有太多經驗,也許這可能是一個愚蠢的問題。我在第22行出現錯誤「無效的過程調用或參數:'Mid'」。任何想法爲什麼會這樣? – Gerardo

+0

在例如一個空字符串可以做到這一點......嘗試第22行之前的'MsgBox sourceLines(i)'來診斷問題。 –

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 = "(@[^@][email protected])\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 

輸出:

@[email protected]=E:/SonicDataFiles/AR_INT/GPP_VE/IN 
@[email protected]=jdbc:sonic:sqlserver://CARASETMS:1433;databaseName=CRM 

--------------- 
@[email protected]= 
@[email protected]= 

--------------- 
jdbc:sonic:sqlserver://CARASETMS:1433;databaseName=CRM= 
@[email protected]= 

是否能解決原則的問題,我們可以敲定細節。

新增:

正如讓 - 弗朗索瓦·科貝特無疑是正確的,使用模式

reCut.Pattern = "(@[^@][email protected]=)(.*?)\s*$" 

,並最終替換

sAll = Replace(sAll, sKey, sKey & dicRpl(sKey)) 

此版本假定在這兩個文件嚴格@[email protected]=[y]格式。

+0

+1比我的優雅得多!我認爲OP希望輸出格式與我在答案底部寫的一樣。 –

相關問題