GDoe
GDoe

Reputation: 37

Read a file's data within a specified range using VB script. Is it possible?

This is the middle of the code I'm trying to work with. Is there a way to make the file it's reading open and read from line 2 to line 97? Where I need the correction is starred (****). What I'm trying to do is get the data from lines 2 through 97 to compare to another file I'll have to open from the same lines. The beginning and ends of each file are different but the middle information should match thus I need these specific lines.

' Build Aliquot file name
strFile = aBarcodeExportDir & "A-" & yearStr & "-" & splitStr2(0) & ".csv"
'msgbox("open file: " & strFile)

If (objFS.FileExists(strFile)) Then
    ' Open A file
    Set objFile = objFS.OpenTextFile(strFile)

    ' Build string with file name minus extension - used later to determine EOF
    strFileNameNoExtension = "A-" & yearStr & "-" & splitStr2(0)

    ' Create dictionary to hold key/value pairs - key = position; value = barcode
    Set dictA = CreateObject("Scripting.Dictionary")

    ' Begin processing A file
    Do Until objFile.AtEndOfStream(*****)

        ' Read a line
        strLine = objFile.ReadLine(*****)

        ' Split on semi-colons
        splitStr = Split(strLine, ";")

        ' If splitStr array contains more than 1 element then continue
        If(UBound(splitStr) > 0) Then

            ' If barcode field is equal to file name then EOF
            If(splitStr(6) = strFileNameNoExtension) Then
                ' End of file - exit loop
                Exit Do
            Else
                ' Add to dictionary
                ' To calculate position
                ' A = element(2) = position in row (1-16)
                compA = splitStr(2)
                ' B = element(4) = row
                compB = splitStr(4)
                ' C = element(5.1) = number of max positions in row
                splitElement5 = Split(splitStr(5), "/")
                compC = splitElement5(0)
                ' position = C * (B - 1) + A
                position = compC * (compB - 1) + compA
                barcode = splitStr(6) & ";" & splitStr(0) & ";" & splitStr(1) & ";" & splitStr(2)

                'msgbox(position & ":" & barcode)

                ' Add to dictionary
                dictA.Add CStr(position), barcode

            End If

        End If  
    Loop

    ' Close A file
    objFile.Close

Upvotes: 1

Views: 243

Answers (1)

Gurmanjot Singh
Gurmanjot Singh

Reputation: 10360

To give the exact answer, we may have to look at your text files(I mean with all the split functions you are using). But, If you just want to compare lines 2-97 of two text files, you can get a hint from the following piece of code:

strPath1 = "C:\Users\gr.singh\Desktop\abc\file1.txt"          'Replace with your File1 Path
strPath2 = "C:\Users\gr.singh\Desktop\abc\file2.txt"          'Replace with your File2 Path     
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFso.OpenTextFile(strPath1,1)
Set objFile2 = objFso.OpenTextFile(strPath2,1)

blnMatchFailed = False
Do Until objFile1.AtEndOfStream
    If objFile1.Line=1 Then
        objFile1.SkipLine()                         'Skips the 1st line of both the files
        objFile2.SkipLine()
    ElseIf objFile1.Line>=2 And objFile1.Line<=97 Then
        strFile1 = objFile1.ReadLine()
        strFile2 = objFile2.ReadLine()
        If StrComp(strFile1,strFile2,1)<>0 Then     'textual comparison. Change 1 to 0, if you want binary comparison of both lines
            blnMatchFailed = True
            intFailedLine = objFile1.Line
            Exit Do                                 'As soon as match fails, exit the Do while Loop
        Else
            blnMatchFailed = False
        End If
    Else
        Exit Do
    End If
Loop

If blnMatchFailed Then
    MsgBox "Comparison Failed at line "&intFailedLine
Else
    MsgBox "Comparison Passed"
End If
objFile1.Close
objFile2.Close
Set objFile1 = Nothing
Set objFile2 = Nothing
Set objFso = Nothing

Upvotes: 0

Related Questions