VBS快速遍历线路

时间:2019-03-29 17:38:31

标签: vbscript

我需要循环和排列文本文件的2000行(该文件将始终增加大小),获取总长度,并根据长度将这两个记录复制到另一个文件中。 问题在于处理所有内容都需要很长时间。我不确定这是否是最好的方法,但会有所帮助。

filename = "Jul2017.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)

Do Until f.AtEndOfStream
    r1 = f.ReadLine
    Do Until f.AtEndOfStream
        r2 = f.ReadLine
        if len(r1 & r2) > 17 then
           'Do something
        end if
    Loop
Loop

WScript.Echo "Done!"
f.Close

这应该解决循环嵌套问题。

filename = "Jul2017.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename, 1)

For x = 1 to 2000
r1 = f.ReadLine
For z = 1 to 2000
r2 = f.ReadLine
if len(r1 & r2) > 17 then
'Do something
end if
next
next
WScript.Echo "Done!"
f.Close

Input
-----------
TMM87R2
YUU52R7VVB
VLL73IOP3
TMM54Y2
VLL21CSZ
YUU56
VLL71BVR54
...

我需要做什么:

First iteration
TMM87R2 & TMM87R2 < 17 characters ( do nothing )
TMM87R2 & YUU52R7VVB > 17 characters ( copy the lines )
TMM87R2 & VLL73IOP3 etc.
...
TMM87R2 & VLL71BVR54

Second iteration
YUU52R7VVB & TMM87R2
YUU52R7VVB & YUU52R7VVB
...

Until last iteration
VLL71BVR54 & VLL71BVR54

每行应“放置”在文件中的每一行旁边,如果总大小超出了17个字符, 将两个记录复制到另一个文件。我知道我要循环2000次,这是重复的,但是记录的顺序很重要。

1 个答案:

答案 0 :(得分:-1)

为什么不将文件读入内存中的数组

这在VB.NET中,但是应该给您一个提示

        Dim lines = File.ReadAllLines("Jul2017.txt")

        Rem Arrays are zero based, and we cant compare the last element with anything so ...
        For first = 0 To lines.Length - 2
            Dim line = lines(first)
            Dim len = line.Length
            For perm = first + 1 To lines.Length - 1
                If lines(perm).Length + len > 17 Then
                    Rem Do Something
                    Console.WriteLine(line & " & " & lines(perm))
                End If
            Next
        Next
相关问题