Как написать n-ю строку ниже указанного c текста в VBS - PullRequest
0 голосов
/ 08 февраля 2020

В текстовом файле, состоящем из тысяч записей, каждая из которых содержит более 20 строк данных, мне нужно подсчитать 14-ю строку после начала каждой записи, если эта 14-я строка пуста. Строка либо пуста, либо содержит дату.

Начало каждой записи одинаковое: «1 Начало новой записи»

Сценарий:

1 Начало новой запись 2 некоторые данные 3 " 4 " 5 " 6 " 7 " 8 " 9 " 10 " 11 " 12 " 13 " 14 ... 1 Начало новой записи ... 8 " 9 " 10 " ... 14 19.10.2009 ...

В этом простом сценарии результат должен быть 1. У меня есть код, который копирует строку 1 каждой записи во второй файл.

Результат, очевидно, такой:

1 Начало новой записи 1 Начало новой записи ...

Вот код, который у меня есть:

Const ForReading = 1

Dim words(1)

Dim msg

words(0) = "1  Start of New Record"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set inFile = objFSO.OpenTextFile("c:\Temp\altest.txt", ForReading)

Set outFile = objFSO.OpenTextFile("c:\Temp\altest_output.txt", 8, True)

Do Until inFile.AtEndOfStream

    strSearchString = inFile.ReadLine

    For i = 0 To UBound(words)-1

    If InStr(strSearchString,words(i)) Then

   msg = msg&strSearchString&vbcrlf

    End If

    next

Loop

inFile.Close

outfile.WriteLine msg

WScript.Echo "Done!"

Это кажется хорошим началом, но опять же, мне нужно посчитать 14-ю строку после начала каждой записи, если это 14-я строка пуста.

Любая помощь с благодарностью. -Alel

1 Ответ

0 голосов
/ 13 февраля 2020

Вряд ли элегантно, но что-то вроде этого должно помочь вам в этом. Здесь не используется SkipLine, просто отмечается следующая интересная строка:

Option Explicit 'force explicit variable declaration, this is just good practice

Const ForReading = 1

Dim strContent
Dim Offset : Offset = 14 'define the 14th 'line'
Dim StartLine

Dim NewRecordMarker : NewRecordMarker = "1 Start of new record" 'just use a string to match

Dim objFSO, inFile, outFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set inFile = objFSO.OpenTextFile("e:\Temp\altest.txt", ForReading)
Set outFile = objFSO.OpenTextFile("e:\Temp\altest_output.txt", 8, True)

'notice we're only reading forward
'that means we can set the next LineOfInterest without having to worry about
'exceeding AtEndOfStream like we would if we'd use SkipLine
'this is just simpler.
'this obviously falls apart when the line of interest is NOT the 14th line
Do Until inFile.AtEndOfStream
    Dim LineOfInterest
    strContent = inFile.ReadLine 'inFile.Line will at 2 at this point because we just read it

    If strContent = NewRecordMarker Then 'found a new record, we want to look 14 lines from here
        LineOfInterest = inFile.line - 1 + Offset ' -1 or we'll overshoot our target
    End If

    If inFile.Line = LineOfInterest Then 'this is the line we want to inspect
        outFile.WriteLine strContent 'just write out entire value, no checking for date here
    End If
Loop

inFile.Close
outFile.Close

WScript.Echo "Done!"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...