VB 脚本在文本文件中查找特定字符串并将文本文件移动到另一个路径 [英] VB script to find a particular string in text file and move the text file to another path

查看:29
本文介绍了VB 脚本在文本文件中查找特定字符串并将文本文件移动到另一个路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need a VBs to find (string) "ERROR" from my alert log.txt and if the alert log.txt has error string , the that particular log must be moved to another specified location . Am totally new to VBs, so please do help me....

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

this is code wich i used(googled it)..

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 字符串.我希望它是静态的.

Here it asks dynamically for d string to be identified. I want it to be static.

推荐答案

因为你的输入来自用户,你必须把它转换成小写,因为你的测试用户文本是否存在使用小写.

because your input comes from the user, you must convert it lower case, simply because your test to see if the user text exists uses lower case.

因此更改以下行.

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

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

这篇关于VB 脚本在文本文件中查找特定字符串并将文本文件移动到另一个路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆