vbscript替换文本-一个工作两个中断整个过程
我希望此脚本替换同一文本文件中的两个值,而不是一个值。但是,如果我取消注释第12行,它会破坏脚本。我必须把它做成一个循环,还是可以做多次替换vbscript替换文本-一个工作两个中断整个过程,vbscript,Vbscript,我希望此脚本替换同一文本文件中的两个值,而不是一个值。但是,如果我取消注释第12行,它会破坏脚本。我必须把它做成一个循环,还是可以做多次替换 Sub ReplaceTxt() 'Writes values we got earlier to our unattend file ' Const ForReading = 1 Const ForWriting = 2 Set objFSO = CreateObject("Scripting.FileSystemObject")
Sub ReplaceTxt()
'Writes values we got earlier to our unattend file '
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strSIFpath, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "***COMPNAME***", strCompname)
' strNewText = Replace(strText, "***Winkey***", strPoductkey) '
Set objFile = objFSO.OpenTextFile("C:\$WIN_NT$.~BT\winnt.sif", ForWriting)
objFile.WriteLine strNewText
objFile.Close
End Sub
我认为您需要对第一个字符串返回的字符串进行第二次替换:
strNewText = Replace(strText, "***COMPNAME***", strCompname)
strNewText = Replace(strNewText , "***Winkey***", strPoductkey)
否则,您将丢失第一次替换,结果中将只显示第二次替换。尝试以下操作:
Sub ReplaceTxt() 'Writes values we got earlier to our unattend file'
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strSIFpath, ForReading)
strText = objFile.ReadAll
objFile.Close
strText = Replace(strText, "COMPNAME", strCompname)
strText = Replace(strText, "Winkey", strPoductkey)
Set objFile = objFSO.OpenTextFile("C:\$WIN_NT$.~BT\winnt.sif", ForWriting)
objFile.WriteLine strText
objFile.Close
End Sub
按照原来的方式做,您使用了原始的、未使用的文本两次,在使用第二次替换时覆盖了第一次替换。我确信我的if语句对真正的程序员来说是难看的,但下面是我如何让它工作的
Sub ReplaceTxt() 'Writes values we got earlier to our unattend file'
Const ForReading = 1
Const ForWriting = 2
counter = 1
For Each searchterm In Array("COMPNAME", "Winkey")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strSIFpath, ForReading)
strText = objFile.ReadAll
objFile.Close
If counter < 2 Then
strText = Replace(strText, searchterm, strCompname)
Else
strText = Replace(strText, searchterm, strProductKey)
End If
Set objFile = objFSO.OpenTextFile("C:\$WIN_NT$.~BT\winnt.sif", ForWriting)
objFile.WriteLine strText
objFile.Close
counter = counter + 1
Next
End Sub
Sub-ReplaceTxt()'将先前获得的值写入无人参与文件'
常数ForReading=1
写入常数=2
计数器=1
对于数组中的每个搜索项(“COMPNAME”、“Winkey”)
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
设置objFile=objFSO.OpenTextFile(strSIFpath,ForReading)
strText=objFile.ReadAll
objFile.Close
如果计数器<2,则
strText=Replace(strText、searchterm、strCompname)
其他的
strText=Replace(strText、searchterm、strProductKey)
如果结束
设置objFile=objFSO.OpenTextFile(“C:\$WIN\u NT$.~BT\winnt.sif”,用于写入)
objFile.WriteLine strText
objFile.Close
计数器=计数器+1
下一个
端接头
好吧。。那很容易。。布埃诺·尼·塔托。。我曾经试着这么做过。我找到了答案:
Sub ReplaceTxt()
'Writes values we got earlier to our unattend file '
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strSIFpath, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "***COMPNAME***", strCompname)
strNewText2 = Replace(strNewText, "***Winkey***", strPoductkey) '
Set objFile = objFSO.OpenTextFile("C:\$WIN_NT$.~BT\winnt.sif", ForWriting)
objFile.WriteLine strNewText2
objFile.Close
End Sub
谢谢。来自墨西哥锡那罗亚库利亚坎的何塞·维拉objFile.WriteLine strNewText应该是strtext谢谢-缺少咖啡时刻:D这两个都做同样的事情,第一个替换有效,第二个为空。你应该说明你想打印出每个替换,否则如果你想同时替换两个,然后打印出来,弗雷德里克的代码应该对你有效。