Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VBScript copy
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
Posted 9-Jan-13 6:50am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
Dustin Prevatt at 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 at 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 at 9-Jan-13 13:17pm
   
However the code does seem to be working!!
Dustin Prevatt at 9-Jan-13 13:42pm
   
That was the issue! Thank You for your help!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 355
1 _Amy 235
2 Peter Leow 185
3 Andreas Gieriet 180
4 Dave Kreskowiak 155
0 OriginalGriff 7,540
1 Sergey Alexandrovich Kryukov 6,462
2 Maciej Los 3,849
3 Peter Leow 3,653
4 CHill60 2,712


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100