Vbscript帮助 - 逐行读取文本文件并填充excel中每行的内容并搜索并计算重复字符串

时间:2013-05-26 03:28:21

标签: vbscript

我想逐行读取文本文件(使用readline函数而不是readall)来搜索文本文件中的重复字符串(例如“嗨,你好吗?”)。用户提供字符串和文本文件路径作为输入。我希望在输出excel中填充以下输出: 1.测试数据(您在文本文件中搜索的字符串,多次出现) 2.contents每行应填入一列 3.如果在任何行中找到测试数据,则在输出excel文件的另一列中产生“Y”,否则为“N” 4.line index(表示第一行,它是1,依此类推)。

我在网上搜索并相应地制作了我的代码,但是我无法在整个文本文件中循环读取线功能,并且我无法生成结果输出excel中每行的内容。如果你善意,我将非常感激。在代码中帮助我。

Sub main()


'creating new excel file in date-time format for gathering output

Dim a
a = Now
Dim strFileName

a = Replace(a, "/", "_")
a = Replace(a, ":", "_")
a = Replace(a, " ", "_")

strFileName = "search output_" & a & ".xls"

Set objExcel = CreateObject("Excel.Application")
'objExcel.Visible = True
Set objWorkBook = objExcel.Workbooks.Add()
Set Objsheet = objWorkBook.Sheets(1)
Row = 4
Objsheet.Cells(Row, 2) = "Test data"
Objsheet.Cells(Row, 4) = "Total row count"
Objsheet.Cells(Row, 6) = "Each row Index"
Objsheet.Cells(Row, 8) = "Row content"
Objsheet.Cells(Row, 10) = "Test data found ?" & vbNewLine & "Y" & "or" & "N"
Objsheet.Cells(Row, 12) = "Count of test data in the row"
Objsheet.Cells(Row, 14) = "Reported by"




Dim Str_input, strTextFile                                           'declaring variables

Str_input = Trim(TextBox_String.Text)                                'user providing input search string

strTextFile = TextBox_Path.Text                                      'User providing text file path

Const ForReading = 1


'contents = ObjFile.readall
'contents = ObjFile.ReadLine

'i = 0
'linesArray = Split(contents, Chr(10))
'For Each lineStr In linesArray
'If InStr(lineStr, Str_input) Then
      'i = i + 1
   'End If
'Next

'ObjFile.Close

Set ObjFso = CreateObject("Scripting.FileSystemObject")
Set ObjFile = ObjFso.OpenTextFile(strTextFile, ForReading)
Content = ObjFile.Readall
Do While Not ObjFile.AtEndOfStream
contents = ObjFile.ReadLine
MsgBox "the output" & Content
Loop

'Set fso = CreateObject("Scripting.FileSystemObject")
'Set listFile = fso.OpenTextFile(aniLines).Readall
'Do While Not listFile.AtEndOfStream
    'fName = listFile.ReadLine()
    'WScript.Echo fName
    'Loop
'Next

Row = 6

Objsheet.Cells(Row, 2) = Str_input
Objsheet.Cells(Row, 4) = ObjFile.Line - 1                                     'No of rows in total
Objsheet.Cells(Row, 6) = " Each row index"
Objsheet.Cells(Row, 8) = "Row content"
Objsheet.Cells(Row, 10) = "Test data found ?" & "Y" & "or" & "N"
Objsheet.Cells(Row, 12) = "Count of test data in the row"
Objsheet.Cells(Row, 14) = "Alex Rider"

objWorkBook.SaveAs "D:\" & strFileName                                           'saving the output excel

Set objExcel = Nothing: Set objBook = Nothing: Set Objsheet = Nothing

End Sub

1 个答案:

答案 0 :(得分:1)

Content = ObjFile.Readall

到达文件/流的末尾,以便

Do While Not ObjFile.AtEndOfStream

循环不会被输入。