列出文件夹中的所有文本文件并将信息写入 Excel 工作表

发布于 2024-11-18 16:58:54 字数 231 浏览 3 评论 0原文

文件夹中有某些文本文件,即 file1.txt ;文件2.txt; ...此外,还有一个字符串数组,可以在 VBScript 中存储为studs(i)。我的目标是验证每个字符串 (studs(i)) 是否存在于每个文本文件 (fileN.txt) 中,并将信息写入 Excel 工作表,其中行和文件名中包含字符串元素 (file1, file2, file3) ,....) 在列中。我需要一个 Vbscript 来自动执行此过程。非常感谢任何帮助

There are certain text files in a folder, i.e, file1.txt ; file2.txt; ... Also, there is an array of strings which can be stored as studs(i) in VBScript. My objective is to verify if each string (studs(i)) is present in each of the text file (fileN.txt) and write the information to an excel sheet with the strings elements in the rows and filenames(file1, file2, file3,....) in the columns. I need a Vbscript which automates this process. Any help is greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶茶白久 2024-11-25 16:58:54

看看这是否有助于

  • 执行findstr命令
  • 捕获System.Collections.ArrayList中的结果
  • 然后可以将结果存储在excel中

代码

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

输出

表格

See if this helps

  • execute findstr command
  • capture result in System.Collections.ArrayList
  • result can then be stored in excel

Code

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

output

table

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文