使用VB脚本读取指定范围内的文件数据。可能吗?

时间:2017-07-04 18:05:51

标签: vbscript

这是我正在尝试使用的代码的中间部分。有没有办法让文件读取打开并从第2行读到第97行?我需要修正的地方是星号(****)。我要做的是从第2行到第97行获取数据,以便与我必须从同一行打开的另一个文件进行比较。每个文件的开头和结尾都不同,但中间信息应该匹配,因此我需要这些特定的行。

' 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

1 个答案:

答案 0 :(得分:0)

要给出确切的答案,我们可能需要查看您的文本文件(我的意思是您正在使用的所有拆分功能)。但是,如果您只想比较两个文本文件的第2-97行,您可以从以下代码中获得提示:

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