Loops 尝试用从vbscript中的其他文件读取的值替换文件中的字符串

Loops 尝试用从vbscript中的其他文件读取的值替换文件中的字符串,loops,vbscript,scripting,Loops,Vbscript,Scripting,我不熟悉编写脚本,我不知道如何用从不同文件读取的值替换文件中的字符串。我曾尝试将这些值存储在数组中,然后将它们插入replace方法,但无法使循环正常工作。以下是我的两个尝试: Const ForReading=1 Const Inc=0 Set objFSO=CreateObject(“Scripting.FileSystemObject”) 设置objTextFile=objFSO.OpenTextFile\u (“readinfle.txt”,用于阅读) 直到objTextFile.AtE

我不熟悉编写脚本,我不知道如何用从不同文件读取的值替换文件中的字符串。我曾尝试将这些值存储在数组中,然后将它们插入replace方法,但无法使循环正常工作。以下是我的两个尝试:

Const ForReading=1
Const Inc=0 Set objFSO=CreateObject(“Scripting.FileSystemObject”)
设置objTextFile=objFSO.OpenTextFile\u
(“readinfle.txt”,用于阅读)
直到objTextFile.AtEndOfStream
strNextLine=objTextFile.Readline
arrServiceList=Split(strNextLine,“,”)
环
设置objFS=CreateObject(“Scripting.FileSystemObject”)
strFile=“OrigFile.txt”
设置objFile=objFS.OpenTextFile(strFile)
直到objFile.AtEndOfStream
strLine=objFile.ReadLine
如果InStr(strLine,“777”)>0,则
strLine=Replace(strLine,“777”,arrServiceList(Inc))Inc=Inc+1;
如果结束
环

Const ForReading=1
设置fso=CreateObject(“Scripting.FileSystemObject”)
设置answers=fso.OpenTextFile(“OrigFile.txt”,用于阅读)
设置fdf=fso.OpenTextFile(“readinfle.txt”,用于读取)
Set computers=CreateObject(“Scripting.Dictionary”)
直到回答为止。AtEndOfStream或fdf。AtEndOfStream
计算机。添加name.ReadLine、locations.ReadLine
环
名字,结束
地点,关闭

我还尝试了一个for-each循环来读取数组,但我无法将它与另一个循环合并。

我进行了一些重建,并添加了注释,以帮助您进行操作。请试着理解每一步

Option Explicit     ' this makes sure you don't forget to declare variables
Const ForReading = 1 
Dim objFSO, objTextFile
Dim serviceCollection, service, strNextLine
Dim i    ' i is a variable, it get incremented. So don't make it a const
Dim strLine, objReadFile, objWriteFile

Set objFSO = CreateObject("Scripting.FileSystemObject") 
' An ArrayList let you .Add items to the collection without redimming it like a
' native array
Set serviceCollection = CreateObject("System.Collections.ArrayList")

Set objTextFile = objFSO.OpenTextFile("ReadInFile.txt", ForReading)    

Do Until objTextFile.AtEndOfStream ' Reads an textfile from start to end
    strNextLine = objTextFile.Readline 
    for each service in Split(strNextLine , ",")   ' This splits a single line into parts on the comma character
        serviceCollection.Add service
    Next
Loop 

' Make an array from all services
arrServiceList = serviceCollection.ToArray()

Set objReadFile = objFSO.OpenTextFile("OrigFile.txt")
Set objWriteFile = objFSO.CreateTextFile("NewFile.txt", true)

i = 0
Do Until objReadFile.AtEndOfStream
    strLine = objReadFile.ReadLine

    If InStr(strLine,"777")> 0 Then
        ' This next line is not totally safe, because arrServiceList can go over its upperbound
        strLine = Replace(strLine, "777", arrServiceList(i))
        i = i + 1;
    End If 
    ' Important! Write the line to the new textfile
    objWriteFile.WriteLine strLine
Loop

' It is good practice to close opened files
objReadFile.Close
objWriteFile.Close
但您仍然会遇到一些问题:当一行上有多个
777
s时,它们会被相同的变量替换,因为
i
在平均时间内不会递增。你可以用类似的东西替换它来解决这个问题

Do Until objReadFile.AtEndOfStream
    strLine = objReadFile.ReadLine
    do
        If InStr(strLine,"777")> 0 Then
            strLine = Replace(strLine, "777", arrServiceList(i), 1, 1)
            i = i + 1;
            thereWasAReplace = True         '  do not forget to declare
        else
            thereWasAReplace = False
        End If 
    Loop While thereWasAReplace
    ' Write the line to the new textfile
    objWriteFile.WriteLine strLine
Loop

免责声明:我是从头开始做的,所以你可能会遇到一两个小错误

条件“replacedLine=strLine”永远不会为真,因为replacedLine在循环中不会更改,strLine也不会变成“”。@Ekkehard.Horner啊,是的,你说得太对了。将其替换为更简单、更易于理解的机制。请发布(简短)输入和(所需)输出文件的示例。