【问题标题】:vbscript search string in multiple files多个文件中的vbscript搜索字符串
【发布时间】:2015-02-19 14:05:58
【问题描述】:

请告知如何更改当前单个传入日志文件以搜索多个文件。

   Dim strTextToFind, strInputFile, strOutputFile, boolMatchCaseSensitive
   Dim objFSO, objInputFile, strFoundText, strLine, objOutputFile 


     strTextToFind = Inputbox("Enter the text you would like to search for.")
    strInputFile = "C:\Users\mmmanima\Desktop\mani\Day_16.txt"

如果你能注意到,我只能访问 day_16 文件

strOutputFile = "C:\Users\mmmanima\Desktop\texting As\result.txt"
   Set objFSO = CreateObject("Scripting.FilesystemObject")
   Const intForReading = 1
   Const intForWriting = 2
   Const intForAppending = 8
   Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)

Do until objInputFile.atEndOfStream
    strLine = objInputFile.ReadLine
    If InStr(strLine,strTextToFind) > 0 Then 
        strFoundText = strLine 
     If strFoundText <> "" Then
            Set objOutputFile = objFSO.OpenTextFile(strOutputFile,intForAppending, True)
                objOutputFile.WriteLine strFoundText
                objOutputFile.Close
                Set objOutputFile = Nothing
         End If
End If 


     loop
  objInputFile.Close
 Set objInputFile = Nothing

 WScript.Quit

在共享文件夹中搜索用户输入字符串需要 VBScript,共有 60 个文件。

【问题讨论】:

    标签: vbscript wsh


    【解决方案1】:

    我相信您想搜索特定文件夹中的所有文件。然后我建议你在读取所有文件时循环你的动作 这样做更容易维护子或功能

    伪:

    var inputFolder = ".\myfolder"
    foreach file in the inputFolder
    {
        inputFile = file
        searchIn(inputFile)
    }
    
    sub searchIn(inputFile)
    {
        'do your current works here
    }
    

    代码:

    这部分会给你所有的文件名

    Set fso = CreateObject("Scripting.FileSystemObject")
    inputFldr = Replace(wscript.scriptfullname,wscript.scriptname,".\")
    Set fldr = fso.getFolder(inputFldr)
    
    For Each file In fldr.Files
     'call to your function
    Next
    

    ---------请注意错别字------

    Dim strTextToFind, strInputFile, strOutputFile, boolMatchCaseSensitive
    Dim objFSO, objInputFile, strFoundText, strLine, objOutputFile 
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    inputFldr = Replace(wscript.scriptfullname,wscript.scriptname,".\")
    Set fldr = objFSO.getFolder(inputFldr)
    
    strTextToFind = Inputbox("Enter the text you would like to search for.")
    
    For Each file In fldr.Files
        yourFunctionName(file )
    Next
    
    sub yourFunctionName(inputFile)
        strInputFile = inputFile    
        strOutputFile = ".\result.txt"
        Const intForReading = 1
        Const intForWriting = 2
        Const intForAppending = 8
        Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
    
        Do until objInputFile.atEndOfStream
            strLine = objInputFile.ReadLine
            If InStr(strLine,strTextToFind) > 0 Then 
                strFoundText = strLine 
                If strFoundText <> "" Then
                    Set objOutputFile = objFSO.OpenTextFile(strOutputFile,intForAppending, True)
                    objOutputFile.WriteLine strFoundText
                    objOutputFile.Close
                    Set objOutputFile = Nothing
                End If
            End If 
        loop
        objInputFile.Close
        Set objInputFile = Nothing
    end sub
    WScript.echo "done"
    WScript.Quit
    

    【讨论】:

      【解决方案2】:

      你可以试试这个vbscript,我加了一个函数BrowseForFolder()

      Option Explicit
      Dim strTextToFind,inputFldr,strInputFile,strOutputFile,path,fldr
      Dim objFSO, objInputFile,strFoundText,strLine,objOutputFile,file,ws 
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set ws = CreateObject("wscript.Shell")
      path = objFSO.GetParentFolderName(wscript.ScriptFullName)
      strOutputFile = path & "\result.txt"
      
      If objFSO.FileExists(strOutputFile) Then
          objFSO.DeleteFile(strOutputFile)
      End if
      
      inputFldr = BrowseForFolder()
      Set fldr = objFSO.getFolder(inputFldr)
      strTextToFind = Inputbox("Enter the text you would like to search for it !","Enter the text you would like to search for it !","wscript")
      
      For Each file In fldr.Files
          Call Search(file,strTextToFind)
      Next
      ws.run strOutputFile
      '***************************************************************************************************************
      Sub Search(inputFile,strTextToFind)
          strInputFile = inputFile
          Const intForReading = 1
          Const intForWriting = 2
          Const intForAppending = 8
          Set objInputFile = objFSO.OpenTextFile(strInputFile,intForReading, False)
          Do until objInputFile.atEndOfStream
              strLine = objInputFile.ReadLine
              If InStr(strLine,strTextToFind) > 0 Then 
                  strFoundText = strLine 
                  If strFoundText <> "" Then
                      Set objOutputFile = objFSO.OpenTextFile(strOutputFile,intForAppending, True)
                      objOutputFile.WriteLine "The Path of file ===> "& DblQuote(strInputFile) & VbCRLF &_
                      "String found "& DblQuote(strTextToFind) & " ===> "& DblQuote(strFoundText) & VbCRLF & String(100,"*")
                      objOutputFile.Close
                      Set objOutputFile = Nothing
                  End If
              End If 
          loop
          objInputFile.Close
          Set objInputFile = Nothing
      End sub
      '***************************************************************************************************************
      Function BrowseForFolder()
          Dim ws,objFolder,Copyright
          Set ws = CreateObject("Shell.Application")
          Set objFolder = ws.BrowseForFolder(0,"Choose the folder to search on it ",1,"c:\Programs")
          If objFolder Is Nothing Then
              Wscript.Quit
          End If
          BrowseForFolder = objFolder.self.path
      end Function
      '****************************************************************************************************************
      Function DblQuote(Str)
          DblQuote = Chr(34) & Str & Chr(34)
      End Function
      '*****************************************************
      

      【讨论】:

      • 尊敬的 Hackoo,非常感谢您的帮助。以上 vbscript 工作正常。但是我有疑问要问。 1.可以在两个不同的文件夹中搜索吗? 2.目前如果strTextToFind找不到有windows错误弹窗,能不能改成没有windows错误后重新开始strTextToFind?
      【解决方案3】:

      在解决 Mara Raj 对 Hackoo 脚本的问题这么长时间之后,今天有点晚了,但这里是为任何可能感兴趣的人准备的。在启动脚本时,它会自动删除任何现有的 result.txt 文件。如果脚本随后继续查找“不匹配”,则它无法生成 results.txt 文件,因为它通常会在存在匹配时执行此操作。更正此问题的最简单方法是插入:

      If objFSO.FileExists(strOutputFile) Then
      else
      wscript.echo "No Matches Found"
      wscript.Quit
      end if
      

      在“next”和“ws.run strOutputFile”之间

      【讨论】:

        猜你喜欢
        • 2012-10-07
        • 2011-04-28
        • 1970-01-01
        • 2012-05-17
        • 2021-11-04
        • 1970-01-01
        • 2011-02-04
        • 1970-01-01
        相关资源
        最近更新 更多