在多个文件夹中搜索文本文件

时间:2014-03-28 11:16:38

标签: search vbscript

我正在尝试创建我的第一个VBS脚本而且我迷路了。我需要能够在4个网络路径中搜索特定文本。基本上这是4个单独的LOG文件,只保存文本文件,没有子目录或任何东西。我想如果文本是在任何一个网络路径中找到它只会带来那条路径而不允许其余部分出现。文本文件的名称应该是用户输入驱动的。但是下面的代码不起作用,我不确定为什么......

有没有人可以指出我正确的方向?还是帮帮我吧?我能够打开网络路径,但不知道如何做其余的事情:这是我到目前为止所做的:

Dim fso, folder, file
Dim folderName, searchFileName, renameFileTo

' Parameters
folderName     = "\\servername\c$\Program Files (x86)\LOGS"
searchFileName = "number_SUMMARY.txt"


' Create filesystem object and the folder object
' how the FSO works: http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.84).aspx
Set fso = CreateObject("Scripting.FileSystemObject")  
Set folder = fso.GetFolder(folderName)  

' Loop over all files in the folder until the searchFileName is found
For each file In folder.Files    
    ' See if the file starts with the name we search
    ' how instr works: http://www.w3schools.com/vbscript/func_instr.asp
    If instr(file.name, searchFileName) = 1 Then
        result = MsgBox ("You Found it!", _
    vbAbortRetryIgnore+vbExclamation+vbDefaultButton2, "You Found it")
    Exit For
     End If
Next

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

  1. 处理您的规格。你想在四个恰好是网络路径的特定位置寻找文件x.txt吗?或者你需要在这四个文件夹的任何子目录中找到一个或多个(第一个?)x.txt?你如何获得用户的输入?
  2. 为了提高效率和易用性,我要避免" Shell.Application"当" Scripting.FileSystemObject"作品。如果您需要递归搜索,请考虑转出dir /s /b
  3. (至少):做一些研究,检查是否存在文件'并在你的问题中加入更多代码。
相关问题