简体   繁体   中英

Push log file into SAP BODS with vbscript

I need to look for a string of text in a log file.

When that string of text is found, I want to get the next 4 lines of text immediately after, see my example below:

Example found text string first line after found string second line after found string third line after found string forth line after found string

My code is :

'strFileName = "D:\write123.xls" 
'Set objExcel = CreateObject("Excel.Application") 
'objExcel.Visible = True 
'Set objWorkbook = objExcel.Workbooks.Add() 
'objWorkbook.SaveAs(strFileName) 
'objExcel.Quit 
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("D:\mic.txt",1) 
strFileText = objFileToRead.ReadAll() 
objFileToRead.Close 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFileToWrite = objFSO.OpenTextFile("D:\write123.xlsx", 2) 
objFileToWrite.Write strFileText 
objFileToWrite.Close

any help or any alternative or any Script given would be greatly appreciated, I just want to fetch the content of the log file into my target table .

Thanks chirag

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
dim objFileToRead : Set objFileToRead = objFSO.OpenTextFile("D:\mic.txt",1) 
dim myArray(0)
While Not objFileToRead.AtEndOfStream
    strLine = objFileToRead.ReadLine
    myArray(UBound(myArray)) = strLine
    reDim Preserve myArray(UBound(myArray)+1)
Wend
objFileToRead.Close 
Dim objExcelApp : Set objExcelApp = CreateObject("Excel.Application")
dim myExcel : Set myExcel = objExcel.Workbooks.Add()
With myExcel.Worksheets(1)
    For myLoop = 0 to UBound(myArray)
        .Range("A" & myLoop).Value = myArray(myLoop)
    Next
End With
myExcel.SaveAs Filename:="D:\write123.xlsx"
myExcel.Close
objExcel.Quit
Set objExcel = Nothing
Set objFileToRead = Nothing
Set objFSO = Nothing

What this code does is reads the entire content of the logfile into an array; create an instance of Excel and a workbook; for each row in the array of log entries, enter it into the "A" column; save and close the workbook before quitting Excel. Let me know if you need any more explanation of what's happening here?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM