Vbscript 读取文件';使用VB脚本在指定范围内创建数据。可能吗?
这是我试图使用的代码的中间部分。有没有办法打开正在读取的文件并从第2行读取到第97行?我需要更正的地方用星号(****)表示。我要做的是从第2行到第97行获取数据,与我必须从同一行打开的另一个文件进行比较。每个文件的开头和结尾都不同,但中间的信息应该匹配,因此我需要这些特定的行Vbscript 读取文件';使用VB脚本在指定范围内创建数据。可能吗?,vbscript,Vbscript,这是我试图使用的代码的中间部分。有没有办法打开正在读取的文件并从第2行读取到第97行?我需要更正的地方用星号(****)表示。我要做的是从第2行到第97行获取数据,与我必须从同一行打开的另一个文件进行比较。每个文件的开头和结尾都不同,但中间的信息应该匹配,因此我需要这些特定的行 ' Build Aliquot file name strFile = aBarcodeExportDir & "A-" & yearStr & "-" & splitStr2(0) &a
' 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
为了给出准确的答案,我们可能需要查看您的文本文件(我的意思是使用您正在使用的所有拆分函数)。但是,如果您只想比较两个文本文件的第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
strPath1=“C:\Users\gr.singh\Desktop\abc\file1.txt”替换为您的file1路径
strPath2=“C:\Users\gr.singh\Desktop\abc\file2.txt”替换为您的file2路径
设置objFso=CreateObject(“Scripting.FileSystemObject”)
设置objFile1=objFso.OpenTextFile(strPath1,1)
设置objFile2=objFso.OpenTextFile(strPath2,1)
blnMatchFailed=False
直到objFile1.AtEndOfStream
如果objFile1.Line=1,则
objFile1.SkipLine()'跳过两个文件的第一行
objFile2.SkipLine()
ElseIf objFile1.Line>=2和objFile1.Line要给出确切的答案,我们可能需要查看您的文本文件(我的意思是使用您正在使用的所有拆分函数)。但是,如果您只想比较两个文本文件的第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
strPath1=“C:\Users\gr.singh\Desktop\abc\file1.txt”替换为您的file1路径
strPath2=“C:\Users\gr.singh\Desktop\abc\file2.txt”替换为您的file2路径
设置objFso=CreateObject(“Scripting.FileSystemObject”)
设置objFile1=objFso.OpenTextFile(strPath1,1)
设置objFile2=objFso.OpenTextFile(strPath2,1)
blnMatchFailed=False
直到objFile1.AtEndOfStream
如果objFile1.Line=1,则
objFile1.SkipLine()'跳过两个文件的第一行
objFile2.SkipLine()
ElseIf objFile1.Line>=2和objFile1.Line谢谢你的帮助Kira。我能找到一个工作,利用一些这。:)谢谢你的帮助,基拉。我能找到一个工作,利用一些这。:)