Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

VB
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
Posted

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:

VB
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
 
Share this answer
 
v2
Comments
Dustin Prevatt 9-Jan-13 13:16pm    
When I run it with your suggestion i am getting an error "Input past the end of file". Do you have any ideas what might be causing this error?
TZetlan 9-Jan-13 13:27pm    
I updated the code to include a check for the size of the file. You are likely getting this error due to a zero byte file that is being processed.
Dustin Prevatt 9-Jan-13 13:17pm    
However the code does seem to be working!!
Dustin Prevatt 9-Jan-13 13:42pm    
That was the issue! Thank You for your help!
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900