1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Searching files for a string with VBscript

Discussion in 'Visual Basic ( VB )' started by tekdatt, Apr 27, 2012.

  1. tekdatt

    tekdatt New Member

    Apr 27, 2012
    Likes Received:
    Trophy Points:
    I need to make a script that searches all the text files in a folder for a specific string and if the file has that string, I need to save it do a different folder. I realize this may be a simple task for a real programmer, but I'm just a novice programmer. Please help. I've been able to figure out how to search a file for the string.

    Dim objFSO, strLine, objReadFile
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objReadFile = objFSO.OpenTextFile("C:\temp\Test.txt", 1, False)

    Do Until objReadFile.AtEndOfStream

    strLine = objReadFile.ReadLine
    strLine = trim(strLine)

    if instr(strLine, "123456") Then
    'save this file to folder C:\temp\save\
    End If


    set objFSO = nothing

    I'm just not understanding how to do it for every file in the folder, and once it finds the string, how to save it to the other folder.

Share This Page