VBS 快速循环遍历行
VBS fast loop through lines
我需要循环和排列一个文本文件的 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
文件中的每一行都应该 "placed" 紧挨着每一行,如果总大小超过 17 个字符,
将这两条记录复制到另一个文件中。我知道我循环了 2000 次,它是重复的,但记录的顺序很重要。
为什么不把文件读入内存中的数组
这在 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
我需要循环和排列一个文本文件的 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
文件中的每一行都应该 "placed" 紧挨着每一行,如果总大小超过 17 个字符, 将这两条记录复制到另一个文件中。我知道我循环了 2000 次,它是重复的,但记录的顺序很重要。
为什么不把文件读入内存中的数组
这在 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