Vbscript在文件列表中搜索字符串 [英] Vbscript To search for a string in a list of Files

查看:71
本文介绍了Vbscript在文件列表中搜索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文件夹中的所有文本文件中搜索特定文件夹中的字符串Login Failed,然后如果找到它,我希望它将找到字符串的文件复制到另一个位置。我有以下代码,但我收到错误找不到文件。希望有人可以提供帮助。



I want to search in a particular folder through all the text files in the folder for the string "Login Failed" then if it is found I want it to copy the file where the string is found to another location. I have the below code but I am getting the error "File not found". Hopefully someone can help.

dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell 'instance of the wshSHell object
set objShell = CreateObject("WScript.Shell")

strSearchFor = "Login Failed"
Set oFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\TelnetLogs\Query1"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
For Each objFile in colFiles
    'Wscript.Echo objFile.Name
strFile = "C:\TelnetLogs\Query1\objFile.Name"
    If InStr(oFSO.OpenTextFile(strFile).ReadAll, strSearchFor) > 0 Then
    filesys.CopyFile "c:\TelnetLogs\Query1\objFile.Name", "c:\TelnetLogs\Failures\objFile.Name"
    Else
    WScript.Sleep (100)
END If
Next

推荐答案

根据您的语法,它正在寻找一个名为objFile.Name的文件,而不是将其视为变量。尝试将代码修改为:



Based upon your syntax, it is looking for a file called objFile.Name instead of treating this as a variable. Try modifying your code to:

dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell 'instance of the wshSHell object
set objShell = CreateObject("WScript.Shell")

strSearchFor = "Login Failed"
Set oFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\TelnetLogs\Query1"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
For Each objFile in colFiles
    'Wscript.Echo objFile.Name
    strFile = "C:\TelnetLogs\Query1\" & objFile.Name
    set objFile = objFSO.getFile(strFile)
    if objFile.size > 0 then
        If InStr(oFSO.OpenTextFile(strFile).ReadAll, strSearchFor) > 0 Then
            filesys.CopyFile "c:\TelnetLogs\Query1\" & objFile.Name, "c:\TelnetLogs\Failures\" & objFile.Name
        Else
            WScript.Sleep (100)
        END If
    END If
Next


我强烈建议你在Windows脚本主机中使用这个过时且蹩脚的游戏并切换到真实的东西:PowerShell。自从最近出现第3版以来,它配备了非常不错的,简约但功能齐全的IDE:逐步调试,智能感知,支持任何操作系统和.NET API(使用COM,这也是主要过时的)以及出色的交互式shell和编程模特。



-SA
I would strongly recommend you to quite this obsolete and lame game in Windows scripting host and switch to the real thing: PowerShell. Since recent advent of v.3, it comes with really decent, minimalistic but fully functional IDE: stepwise debugging, intellisense, support for any OS and .NET API (with COM, which is also majorly obsolete) and great both interactive shell and programming model.

—SA


这篇关于Vbscript在文件列表中搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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