文件夹中有某些文本文件,即file1.txt; FILE2.TXT; ...此外,还有一个字符串数组,可以存储为VBScript中的studs(i)。我的目标是验证每个文本文件(fileN.txt)中是否存在每个字符串(studs(i)),并将信息写入excel表,其中包含行和文件名中的字符串元素(file1,file2,file3) ,....)在列中。我需要一个自动执行此过程的Vbscript。非常感谢任何帮助
答案 0 :(得分:1)
看看这是否有帮助
<强>代码强>
Function findFilesThatContain(searchText, filePath)
Set DataList = CreateObject _
("System.Collections.ArrayList")
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("findstr /M """ & searchText & """ " & filePath)
Do While Not objExecObject.StdOut.AtEndOfStream
fileLoc = objExecObject.StdOut.ReadLine()
'Wscript.Echo searchText&","&fileLoc
DataList.Add fileLoc
Loop
Set findFilesThatContain = DataList
End Function
Sub saveToExcel(searchText, searchPath, strExcelPath)
Set objExcel = CreateObject("Excel.Application")
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "Excel application not found."
Wscript.Quit
End If
objExcel.Workbooks.Add
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
objSheet.Name = "Search Result"
Dim i,j
j = 1
For Each searchText in searchTexts
Dim files
Set files = findFilesThatContain (searchText, searchPath)
i = 1
objSheet.Cells(i, j).Value = searchText
For Each path in files
Wscript.Echo searchText&","&path
i = i + 1
objSheet.Cells(i, j).Value = path
Next
j = j + 1
Next
objSheet.Range("1:1").Font.Bold = True
objExcel.ActiveWorkbook.SaveAs strExcelPath, 56
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
End Sub
Dim strExcelPath
strExcelPath = "c:\test.xls"
Dim searchPath
searchPath = "E:\bin\bat\*.bat"
Dim searchTexts(2)
searchTexts(0)="pushd"
searchTexts(1)="if"
saveToExcel searchText, searchPath, strExcelPath
<强>输出强>