我如何解决这个问题"输入文件末尾"错误?

时间:2014-05-01 06:56:41

标签: excel vba excel-vba vbscript

我必须找到包含PGM=FTP的所有步骤然后读取所有行,直到找到另一个没有PGM=FTP的步骤。它可以是除FTP之外的任何内容。我需要将所有行的行写入具有相应程序名称的另一列。

这是一个示例输入:

//GPFCGI1  DD DSN=PHGP.GPFCGIM.CGI.PATH,DISP=SHR
//GPFPWO2  DD DSN=PHGP.GPFMKT2.MKT.ESDS,DISP=SHR
//GPFPWO4  DD DSN=PHGP.GPFMKT4.MKT.ESDS,DISP=SHR
//SYSOUT   DD SYSOUT=*
//SYSIN    DD *
//STEP006  EXEC PGM=FTP
//FILE01   DD DSN=PHGP.GPFMKT2.MKT.ESDS,DISP=SHR
//FILE02   DD DSN=PHGP.GPFMKT4.MKT.ESDS,DISP=SHR
//SYSOUT   DD SYSOUT=*
//SYSIN    DD *
OPEN HKGSPADD.AP.MANULIFE.COM                                    
NOLOG *****Dummy Member Include**********************************
ASCII                                                            
PUT  %FILE01,ESDS,FB,80,80   CXX_TRIAL4_ELIGIBLE_PAC_PAP.CSV     
PUT  %FILE02,ESDS,FB,86,86   CXX_TRIAL4_WORK_ELIGIBLE_PAC_PAP.DAT
BYE                                                              
//STEP007  EXEC PGM=IDCAMS
//SYSPRINT DD SYSOUT=*
//SYSIN    DD *
 DELETE ( PHGP.GPFMKT1.MKT.INT.KSDS ) PURGE CLUSTER
DELETE ( PHGP.GPFMKT2.MKT.ESDS ) PURGE CLUSTER    
DELETE ( PHGP.GPFMKT3.MKT.KSDS ) PURGE CLUSTER    
DELETE ( PHGP.GPFMKT3.MKT.TXT.ESDS ) PURGE CLUSTER
DELETE ( PHGP.GPFMKT4.MKT.ESDS ) PURGE CLUSTER    
//*

我创建了这个Sub,但是它给了我一个“运行时错误62:输入文件末尾”。为什么呢?

Sub Define()
    Dim file As Object
    Dim flag As String
    flag = True
    folder = "D:\DOWNLOADS\New folder"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    J = 1
    'Opening the file in READ mode
    Set FLD = objFSO.GetFolder(folder)
    For Each file In FLD.Files
        Set objFile = objFSO.OpenTextFile(file)
        Do While Not objFile.AtEndOfStream
            If contents Like "*PGM=FTP" Then
                'If InStr(contents, "*PGM=FTP") Then
                flag = True
                contents3 = contents

                Do Until flag = False
                    strline = objFile.Readline
                    If strline Like "//STEP*" Then
                        flag = False
                        contents = strline
                    Else
                        contents3 = contents3 & vbNewLine & strline
                    End If
                Loop
                Sheets(1).Cells(J, 1).Value = file.Name
                Sheets(1).Cells(J, 2).Value = contents3
                J = J + 1
            Else
                contents = objFile.Readline
            End If

        Loop
    Next
    objFile.Close
End Sub

1 个答案:

答案 0 :(得分:2)

你需要一个简单的循环(每回合只需一个.ReadLine以避免读取过去的EOF)和一个跟踪你是否看过FTP线的变量。在代码中:

  Dim tsIn : Set tsIn = goFS.OpenTextFile("..\data\in.txt")
  Dim bFtpSeen : bFtpSeen = False
  Do Until tsIn.AtEndOfStream
     Dim sLine : sLine = tsIn.ReadLine()
     Select Case True
       Case 1 = Instr(sLine, "//STEP") And 12 = Instr(sLine, "EXEC PGM=FTP")
         bFtpSeen = True
         WScript.Echo "FTP", tsIn.Line - 1, sLine
       Case 1 = Instr(sLine, "//STEP") And 12 = Instr(sLine, "EXEC PGM=")
         If bFtpSeen Then
            bFtpSeen = False
            WScript.Echo "other after FTP", tsIn.Line - 1, sLine
         Else
         End If
       Case Else
    End Select
  Loop
  tsIn.Close