vbscript在多个文件中搜索字符串

时间:2015-02-19 14:05:58

标签: vbscript wsh

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

   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个文件。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

你可以尝试这个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
'*****************************************************

答案 2 :(得分:-1)

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

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

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