VB脚本在文本文件中查找特定字符串并将文本文件移动到另一个路径

时间:2013-04-26 09:37:45

标签: vbscript

我需要一个VB来从我的警报log.txt中查找(字符串)“ERROR”,如果警告log.txt有错误字符串,则必须将该特定日志移动到另一个指定位置。 我对VB很新,所以请帮助我....

这是我使用的代码(谷歌搜索)..

Option Explicit 
'* 
Const ForWriting = 8

'* 
Dim strGFI, objFSO1, objOutputFile 
Dim strOTF, MyString, objFileSystem 
Dim strWSE, strWritePath, objFile 
Dim strInput, cFOL, StrDirectory

'* 
cFOL = "C:\temp\"
strInput = Inputbox("Enter the text you would like to search for.")

MyString = "Workstation"
strWritePath = "c:\text" & MyString & ".txt"
strDirectory = "C:\temp\"

Set objFSO1 = CreateObject("Scripting.FileSystemObject")

If objFSO1.FileExists(strWritePath) Then
  'Wscript.Echo "The file Exists" 
Else
  Set objFile = objFSO1.CreateTextFile(strDirectory & MyString & ".txt")
  objFile = ""
End If

Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.OpenTextFile(strDirectory & MyString & ".txt", ForWriting)

If cFOL > "" Then
  If strInput > "" Then
    Dim objFSO 
    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Dim objGFO 
    Set objGFO = objFSO.GetFolder(cFOL) 
    Dim objGFI 
    Set objGFI = objGFO.Files 
    For Each strGFI in objGFI 
      Dim objOTF 
      Set objOTF = objFSO.OpenTextFile(cFOL & "\" & strGFI.Name,1) 
      Do While Not objOTF.AtEndOfStream 
        strOTF = objOTF.ReadAll() 
      Loop 
      objOTF.Close() 
      Set objOTF = Nothing 
      '* 
      If  InStr(LCase(strOTF), strInput) > 0 Then 
        strWSE = strWSE & strGFI.Name & " contains " & strInput & vbCrLf
      Else 
        strWSE = strWSE & strGFI.Name & " Does not contain: " & strInput & vbCrLf 
      End If 
    Next 
    objOutputFile.WriteLine(strWSE)
    objOutputFile.Close

    '* 
    Set objGFI = Nothing 
    Set objGFO = Nothing 
    Set objFSO = Nothing
    Set objFileSystem = Nothing
    Set objFSO1 = Nothing

    '* 
    WScript.Echo strWSE
  Else
    Wscript.Echo "You Clicked Cancel or no search string was defined."
  End If
Else
  Wscript.Echo "You Clicked Cancel or no search path was defined."
  WScript.Quit
End If

这里动态请求识别d字符串。我希望它是静态的。

1 个答案:

答案 0 :(得分:0)

因为您的输入来自用户,您必须将其转换为小写,因为您的测试是否存在用户文本使用小写。

因此,请更改以下行。

If  InStr(LCase(strOTF), strInput) > 0 Then 

If  InStr(LCase(strOTF), LCase(strInput)) > 0 Then