• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2201
  • Last Modified:

VBSCRIPT: "Input past EOF" error

This is a script from www.windowsITpro.com http://www.windowsitpro.com/Article/ArticleID/39111/39111.html that worked for a while and worked great but now has started failing. It has never been modified. I don't know what the problem is. It fails at random points in the file processing with the following error:

Script: \\server\share\buildreport.vbs
Line: 278
Char: 5
Error: Input past end of file
Code: 800A003E
Source: Microsoft VB Script Runtime Error

Line 278 is starred...

I can provide examples of the files being proccessed if needed. I normally wouldn't ask a question this broad or post code like this but I really am at a total loss... Thanks for any help...

**********************START CODE*******************

'Constants used in this script
Const ForReading = 1
Const xlNormal = &HFFFFEFD1
Const xlWBATWorksheet = &HFFFFEFB9
Const xlRight = &HFFFFEFC8
Const xlOtherSessionChanges = 2

'Initialize global variables
strInvFilePath = "\\server\share\profiles\"
strReportPath = "\\server\share\report\"

'New sheet names. This is outside of a specific sub-routine
'because multiple subroutines use it.
arrWBNames = Array("Computer Systems","Page Files","RAM", _
  "SCSI Controllers","IDE Controllers","Disk SerNums", _
  "Removable Media","Fixed Disks","Processors", _
  "NICs","Monitors","Video Adapters","MotherBoards", _
  "BIOS")

'Creat Excel workbook globally because it's used by several routines
Set objExcel = CreateObject("Excel.Application")
Set objWorkBook = objExcel.Workbooks.Add  'new
Call PrepareWorkSheet

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(strInvFilePath)
Set objFiles = objFolder.Files

'intRowPosition = 2 'Inventory data begins to be written to row 2 of each worksheet
For Each File in objFiles
  If Mid(File.Name,1,9) = "HrdWrInv_" Then
    strFileName = File.Name
    'Use the value of computer name for the first instance of each row of data.
    strComputerName = Mid(strFileName,10,(Instr(strFileName,".") - 10))
    WScript.Echo "Processing inventory for " & strComputerName
    Set objTextFile = objFSO.OpenTextFile(strInvFilePath & strFileName,ForReading)
   
    'Using a sepearate text file object because you don't want to put the
    'cursor at the end of the file and the readall method will do this
    'and then you can't use readline, etc. to move through the file's contents.
    Set objFileForSearching = objFSO.OpenTextFile(strInvFilePath & strFileName,ForReading)
    'read the file and use this for determining if a particular section head is
    'found anywhere in the file.
    strTextFile = objFileForSearching.ReadAll
   
    arrItems = Array("SkipLine","SkipLine","Caption","Version")
    Call GenReport("OSInformation:",strTextFile, _
      "CreationClassName: Win32_OperatingSystem",arrItems)
    arrItems = Array("SkipLine","Manufacturer","Model", _
     "SkipLine","NumberOfProcessors","SystemType","TotalPhysicalMemory")
    Call GenReport("ComputerSystem:",strTextFile, _
      "CreationClassName: Win32_ComputerSystem",arrItems)
    arrItems = Array("MaximumSize","Name")
    Call GenReport("PageFileSetting:",strTextFile, _
      "MaximumSize:",arrItems)
    arrItems = Array("SkipLine","MaxCapacity","Tag")
    Call GenReport("PhysicalMemoryArray:",strTextFile, _
      "CreationClassName: Win32_PhysicalMemoryArray",arrItems)
    arrItems = Array("SkipLine","Description","SkipLine","Manufacturer","Name")
    Call GenReport("SCSIController:",strTextFile, _
      "CreationClassName: Win32_SCSIController",arrItems)
    arrItems = Array("SkipLine","Description","SkipLine","Manufacturer","Name")
    Call GenReport("IDEController:",strTextFile, _
      "CreationClassName: Win32_IDEController",arrItems)
    arrItems = Array("SkipLine","SerialNumber","SkipLine")
    Call GenReport("PhysicalMedia:",strTextFile, _
      "CreationClassName: Not available",arrItems)
    arrItems = Array("SkipLine","Description","DeviceID","SkipLine")
    Call GenReport("LogicalDisk:",strTextFile, _
      "CreationClassName: Win32_LogicalDisk",arrItems)
    arrItems = Array("Caption","SkipLine","Description","SkipLine", _
      "InterfaceType","Manufacturer","MediaType","Model","Partitions", _
      "Size")
    Call GenReport("DiskDrive:",strTextFile, _
      "CreationClassName: Win32_DiskDrive",arrItems)
    arrItems = Array("SkipLine","DeviceID","ExtClock","Manufacturer", _
      "MaxClockSpeed","SkipLine","Revision","Version")
    Call GenReport("Processor:",strTextFile, _
      "CreationClassName: Win32_Processor",arrItems)
    arrItems = Array("AdapterType","SkipLine","SkipLine", _
      "MACAddress","Manufacturer","ProductName")
    Call GenReport("NetworkAdapter:",strTextFile, _
      "CreationClassName: Win32_NetworkAdapter",arrItems)
    arrItems = Array("SkipLine","Description","SkipLine","MonitorType")
    Call GenReport("DesktopMonitor:",strTextFile, _
      "CreationClassName: Win32_DesktopMonitor",arrItems)
    arrItems = Array("AdapterRAM","SkipLine","SkipLine", _
      "MaxRefreshRate","Name")
    Call GenReport("VideoController:",strTextFile, _
      "CreationClassName: Win32_VideoController",arrItems)
    arrItems = Array("SkipLine","Description","Manufacturer", _
      "Product","SkipLine")
    Call GenReport("BaseBoard:",strTextFile, _
      "CreationClassName: Win32_BaseBoard",arrItems)
    arrItems = Array("Manufacturer","Name","SMBIOSBIOSVersion", _
      "SMBIOSMajorVersion","SMBIOSMinorVersion","SoftwareElementID", _
      "SkipLine","SkipLine","Version")
    Call GenReport("BIOS:",strTextFile, _
      "SoftwareElementID",arrItems)
 End If 'End reading inventory file
Next
objWorkBook.Save
objWorkBook.Sheets("Computer Systems").Select        
objExcel.Visible = True

'*****Subroutines and Functions*********
Sub GenReport(SectionHead,TextFile,UniqueLabel,ReportItemsArray)
  Call MoveToSectionHead(SectionHead,TextFile)
  intInstances = FindValues(UniqueLabel,TextFile)
  If intInstances > 0 Then
    For i = 1 to intInstances
      'Determine the next available row in the current worksheet
      intRow = NextRowCount(SectionHead)
      'Move over to the fourth column if the SectionHead is "ComputerSystem:"
      'because this sheet first gets 3 cols of data from the OSInformation section.
      If SectionHead = "ComputerSystem:" Then
        intColPosition = 4
      Else    
        intColPosition = 2
      End If
      For Each Item in ReportItemsArray
        If item = "SkipLine" Then
          'Doing this because you don't want to advance to the next
          'column for a skipped line.
          intColPosition = intColPosition - 1
          objTextFile.SkipLine
        Else
          strItem = ReadVal(Item)
          If strItem = " " Then
            strItem = "Not Listed"
          End If
          Call InsertValues(SectionHead,intRow,intColPosition,strItem,i)
        End If
        intColPosition = intColPosition + 1
      Next
      objTextFile.SkipLine
    Next
  End If
End Sub

'Select the worksheet based on the section of data being read
Function SelectWorkSheet(SectionHead)
  arrSectionNames = Array("OSInformation:ComputerSystem:", _
   "PageFileSetting:","PhysicalMemoryArray:","SCSIController:", _
   "IDEController:","PhysicalMedia:","LogicalDisk:","DiskDrive:", _
   "Processor:","NetworkAdapter:","DesktopMonitor:", _
   "VideoController:","BaseBoard:","BIOS:")
  If SectionHead = "OSInformation:" Or _
   SectionHead = "ComputerSystem:" Then
   SelectWorkSheet = 0
  Else
    i=0
    Do Until arrSectionNames(i) = SectionHead
      i=i + 1
    Loop
    SelectWorkSheet = i
  End If
End Function

Function NextRowCount(SectionHead)
  objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
  If SectionHead = "ComputerSystem:" Then
    NextRowCount = objExcel.ActiveSheet.UsedRange.Rows.Count
  Else
    NextRowCount = objExcel.ActiveSheet.UsedRange.Rows.Count + 1
  End If
End Function

'this is the routine that inserts values in the spreadsheet
Sub InsertValues(SectionHead,RowPosition,ColPosition,Item,Instances)
  Select Case SectionHead
    Case "OSInformation:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
    Case "ComputerSystem:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:H1")
    Case "PageFileSetting:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:C1")
    Case "PhysicalMemoryArray:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:C1")
    Case "SCSIController:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:D1")
    Case "IDEController:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:D1")
    Case "PhysicalMedia:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:B1")
    Case "LogicalDisk:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:C1")
    Case "DiskDrive:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:I1")
    Case "Processor:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:G1")
    Case "NetworkAdapter:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:E1")    
    Case "DesktopMonitor:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:C1")
    Case "VideoController:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:D1")
    Case "BaseBoard:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:D1")
    Case "BIOS:"
      objWorkBook.Sheets(arrWBNames(SelectWorkSheet(SectionHead))).Select
      Call FillCells(ColPosition,RowPosition,Item,Instances)
      Call FormatWorkSheet("A1:H1")                  
  End Select
End Sub

Sub FillCells(ColPosition,RowPosition,Item,Instances)
  'An instance value of 0 means there's nothing to write
  'the section exists in the text file but there's no data
  'in it.
  If Instances = 1 Then
    objExcel.Cells(RowPosition,1).Value = strComputerName
    objExcel.Cells(RowPosition,ColPosition).Value = Trim(Item)  
  ElseIf Instances > 1 Then
    objExcel.Cells(RowPosition,ColPosition).Value = Trim(Item)
  End If
End Sub

Sub MoveToSectionHead(HeadName,TextFile)
  Set objRegExp = New RegExp
  objRegExp.Pattern = HeadName 'Look for this value
  objRegExp.IgnoreCase = False 'Look for value w/specific case
  objRegExp.Global = False 'Find only the first match
  Set arrSectionHead = objRegExp.Execute(TextFile)
  If objRegExp.Test(TextFile) <> False Then
    strText = objTextFile.ReadLine
    objRegExp.Execute(strText)
    Do Until objRegExp.Test(strText) = True
      strText = objTextFile.ReadLine
      objRegExp.Execute(strText)
    Loop
  End  If
End Sub

'This tells you how many values you have in a section of hardware inventory
'data. Knowing this then tells you what you have to read from the section.
Function FindValues(Label,TextFile)
  Set objRegExp2 = New RegExp
  objRegExp2.Pattern = Label 'Look for this value
  objRegExp2.IgnoreCase = False 'Look for value w/specific case
  objRegExp2.Global = True 'Find all matches in this section
  Set arrValues = objRegExp2.Execute(TextFile)
  i = 0
  For Each Value in arrValues
    i = i + 1
  Next
  FindValues = i
End Function

Function ReadVal(Label)
'*****************************LINE 278**********************************
    ReadVal = Mid(objTextFile.ReadLine,Len(Label) + 3)
'*****************************LINE 278**********************************
End Function

Sub PrepareWorkSheet()
  objWorkBook.SaveAs strReportPath & "InvReport.xls",xlNormal
  objExcel.SheetsInNewWorkBook = 14
  'Ensure that the worksheet has enough sheets to run
  'correctly. If not, use the Add method to add worksheets.
  If objWorkBook.Sheets.Count < 14 then
        For i = objWorkBook.Sheets.Count to 14
              objWorkBook.Sheets.Add()
        Next
  End if

   'iterate the array of new sheet names and rename the sheets
  i=1
  For Each WBName in arrWBNames
    RenameSheets "Sheet" & i,WBName
    i = i + 1
  Next
  'Column naming Section
  'Name the Computer Systems columns
  arrColumns = Array("Caption","Version","Manufacturer","Model", _
    "# Processors","System Type","RAM")
  Call NameColumns(0,arrColumns)
  'Name the Page Files columns
  arrColumns = Array("Maximum Size","Name")
  Call NameColumns(1,arrColumns)
  'Name the RAM columns  
  arrColumns = Array("Maximum Capacity","Tag")
  Call NameColumns(2,arrColumns)
  'Name the SCSI controllers columns
  arrColumns = Array("Description","Manufacturer","Name")
  Call NameColumns(3,arrColumns)
  'Name the IDE controllers columns
  arrColumns = Array("Description","Manufacturer","Name")
  Call NameColumns(4,arrColumns)
  'Name the Disk SerNums columns
  arrColumns = Array("Serial Number")
  Call NameColumns(5,arrColumns)
  'Have to do this because Excel interprets a serial
  'number (w/only numbers) as a numeric value rather
  'than a string and then
  'clips the value and uses and exponent designation
  objWorkbook.ActiveSheet.Columns("B:B").Select
  objExcel.Cells.EntireColumn.NumberFormat = "@"
  objExcel.Cells.HorizontalAlignment = xlRight
  objExcel.Range("A1").Select
  'Name the Removable Media columns
  arrColumns = Array("Descripton","Device ID")
  Call NameColumns(6,arrColumns)
  'Name the Fixed Disk columns
  arrcolumns = Array("Description","Device ID","Interface Type", _
    "Manufacturer","Media Type","Model","# of Partitions","Size")
  Call NameColumns(7,arrColumns)
  'Name the Processors columns
  arrColumns = Array("DeviceID","External Clock","Manufacturer", _
    "Maximum Speed","Revision","Version")
  Call NameColumns(8,arrColumns)
  'Name the NICs columns
  arrColumns = Array("Adapter Type","MAC Address", _
    "Manufacturer","Product Name")
  Call NameColumns(9,arrColumns)
  'Name the Monitors columns
  arrColumns = Array("Description","MonitorType")
  Call NameColumns(10,arrColumns)
  'Name the Video Adapters columns
  arrColumns = Array("Adapter Memory","Maximum Refresh Rate","Name")
  Call NameColumns(11,arrColumns)
  'Name the Motherboards columns
  arrColumns = Array("Description","Manufacturer","Product")
  Call NameColumns(12,arrColumns)
  'Name the BIOS columns
  arrColumns = Array("Manufacturer","Name","BIOS Version", _
    "Major Version","Minor Version","Software Element ID", _
    "Version")
  Call NameColumns(13,arrColumns)
  'objWorkBook.Sheets("Computer Systems").Select
  objWorkBook.Save
End Sub

Sub NameColumns(SheetNum,ColumnsArray)
  objWorkBook.Sheets(arrWBNames(SheetNum)).Select  
  i=2
  For Each ColumnHead in ColumnsArray
   'All sheets will have ComputerName as the first column name
   'so hard code it here.
   objExcel.Cells(1,1).Value = "Computer Name"
   objExcel.Cells(1,i).Value = ColumnHead
   i = i + 1
  Next
End Sub
       
'Rename each sheet
Sub RenameSheets(CurrentName,NewName)
  objWorkBook.Sheets(CurrentName).Select
  objWorkbook.ActiveSheet.Name = NewName
End Sub        
 
'This is for formatting each worksheet.
Sub FormatWorkSheet(HeadCellRange)
  objExcel.Range(HeadCellRange).Font.Bold = True
  objExcel.Cells.EntireColumn.AutoFit
  objExcel.Cells.EntireRow.AutoFit
End Sub
0
mbritojr
Asked:
mbritojr
  • 2
1 Solution
 
TazDev00Commented:
If objTextFile.AtEndOfStream = False Then
ReadVal = Mid(objTextFile.ReadLine,Len(Label) + 3)
Else
    '  At the end of file
    ReadVal = " "
End If
0
 
TazDev00Commented:
Oops sorry, posted that accidentally.
Anyway, try replacing the ReadVal code with this.
It just checks for the end of the file before doing any further processing.
I leave it up to you to decide what to do if you're at the end of the file.
0
 
ADSaundersCommented:
Hi,
->    arrItems = Array("SkipLine","SkipLine","Caption","Version")
This is the sort of thing that can cause your problem. Here you are assuming that there are at least three remaining lines in the file at this point, two to be ignored, and one to be processed. If the file at this point does not contain at least three lines, then you will get the error you describe. Whenever i'm reading a file line by line (in VB or VBScript, using FSO or otherwise, I do just that, read it line by line, and check for EOF (or AtEndOfStream) for each line without assumptions. In my opinion, I would probably use ReadLine, and then either ignore it's contents or parse the input linestring:
LineString = objFile.Readline
SkipLines = 2
Do While Not objFile.AtEndOfStream
    SkipLines = SkipLines - 1
    If Skiplines > 0 then Loop
    ' - - - Parse data in line and process - - - <- Only get here if having skipped 2 lines, we are still not at end
    ' Indicate that we actually have processed something
    SkipLines = -1
    LineString = objFile.Readline ' And read the next line (if required)
Loop
' At this point, if SkipLines is anything other than -1 Then we have a short file!

An alternative is to process your actions based on the 'line number' incoming . . .:

LineString = objFile.Readline
Do While Not objFile.AtEndOfStream
    Select Case objFile.Line
        Case 3
              'Process first Line Type
        Case 4
              ' Process Second line type
        Case 6
              ' Process third line type
        ' etc. etc.
        Case Else
              Loop
    End Select
    LineString = objFile.Readline ' And read the next line (if required)
Loop

The above will process Lines 3,4,6 in the file, skipping the others. This is ideal if the number and order of lines in the file is predictable (as yours seeems to be).
In either case, the loop will drop through at end of file (stream) without giving the error experienced.

Regards . . Alan
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now