gpt4 book ai didi

vbscript - 如何使用 vbscript 从文本文件中读取每 20 行?

转载 作者:行者123 更新时间:2023-12-02 22:10:31 24 4
gpt4 key购买 nike

我的文本文件有 180 行,想要每 20 行读取一次(1-20、21-40...)

这是我当前的代码:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objTextFile = objFSO.OpenTextFile("C:\Bess_Automation\EditFiles\TSTVLD1.txt", ForReading)

'Reading the count of lines
objTextFile.ReadAll
strLinecount=objTextFile.Line
msgbox strLinecount

strnumoftimes=Round((strLinecount/20),0)
msgbox strnumoftimes

最佳答案

这是我解决这个问题的方法。此代码首先设置一次要读取的行数,然后打开文件进行读取并设置一个数组。虽然我们还没有读完文件,但我们将其中的一行添加到 myArray 中。

当我们读取 20 行的倍数时,我们会报告该情况,并对这 20 行执行我们需要的操作(在我的例子中,我只是将它们回显到屏幕上,并用分号分隔)。

然后我们将数组再次重置为空并重复,直到读取所有文件,然后输出最后一批行(否则它们将被忽略,因为我们在示例中只对 20 批进行任何操作) .

Option Explicit

Const LINES_TO_READ = 20
Dim iLines, iTotalLines
Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oFile : Set oFile = oFso.OpenTextFile("C:\temp\mytextfile.txt", 1)
Dim myArray()
ReDim myArray(0)
iLines = 0
iTotalLines = 0
While Not oFile.AtEndOfStream
myArray(UBound(myArray)) = oFile.ReadLine
iLines = iLines + 1
ReDim Preserve myArray(UBound(myArray)+1)
If iLines Mod LINES_TO_READ = 0 Then
WScript.Echo iLines & " read now."
' do anything you like with the elements of myArray here before we reset it to empty
WScript.Echo Join(myArray, ";")
' reset array to be totally empty again
ReDim myArray(0)
End If
Wend
WScript.Echo "Final Lines: " & Join(myArray, ";")
WScript.Echo "Total lines in file: " & iLines

关于vbscript - 如何使用 vbscript 从文本文件中读取每 20 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38478016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com