0

我是脚本新手,我不知道如何用从不同文件读取的值替换文件中的字符串。我尝试将值存储在数组中,然后将它们插入替换方法,但我无法让循​​环工作。这是我的两个尝试:

Const ForReading = 1 

Const Inc = 0   Set objFSO = CreateObject("Scripting.FileSystemObject") 

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

Do Until objTextFile.AtEndOfStream 

    strNextLine = objTextFile.Readline 
    arrServiceList = Split(strNextLine , ",")
Loop 

Set objFS = CreateObject("Scripting.FileSystemObject")

strFile = "OrigFile.txt"

Set objFile = objFS.OpenTextFile(strFile)

Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine

    If InStr(strLine,"777")> 0 Then
        strLine = Replace(strLine,"777",arrServiceList(Inc))    Inc = Inc + 1;
    End If 
Loop

Const ForReading = 1

Set fso = CreateObject("Scripting.FileSystemObject")

Set answers     = fso.OpenTextFile("OrigFile.txt", ForReading)

Set fdf = fso.OpenTextFile("ReadInFile.txt", ForReading)

Set computers = CreateObject("Scripting.Dictionary")

Do Until answers.AtEndOfStream Or fdf.AtEndOfStream
  computers.Add names.ReadLine, locations.ReadLine

Loop

names.Close

locations.Close

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

4

1 回答 1

1

我做了一些重建评论,以帮助你。请尝试理解每个步骤。

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

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

于 2013-10-17T14:04:54.593 回答