Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script that Searches for string in text files then executes a file copy not working

Status
Not open for further replies.

raiden69

MIS
Sep 7, 2015
5
0
0
ZA
Any assistance appreciated.

I have a script i cobbled together.

I want the script to search through text files in a specified folder, it must search for a specific string in multiple text files.
If this string is found it must then copy files from one folder to another on the pc it is running.
The script runs without any errors, but the file copy does not execute.

I`ve created a text file that contains the string specified, but still no folder copy

Script :

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

set oShellEnv = objShell.Environment("Process")
computerName = oShellEnv("ComputerName")

WScript.Echo computerName
strSearchFor = computerName

Set oFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Test"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files

For Each objFile in colFiles
Wscript.Echo objFile.Name
strFile = "C:\Test\" & objFile.Name
set objFile = objFSO.getFile(strFile)

If InStr(oFSO.OpenTextFile(strFile).ReadAll, strSearchFor) > 0 Then
objFSO.CopyFolder "C:\test\ssavers\ssavers1" , "C:\screensavers\"
Else
WScript.Sleep (100)
END If
Next

 
Why do you create so many FSO objects?? You only need one.
You also set a file object without using it.
You also ignore that the file object does not only have a name property but also a Path, which is the full path to the file.

I just tested this in VBA, but should also work as vbscript:
Code:
Set filesys = CreateObject("Scripting.FileSystemObject")

objStartFolder = "C:\Test"
Set objFolder = filesys.GetFolder(objStartFolder)
Set colFiles = objFolder.Files

For Each objFile In colFiles
    Set objFile = filesys.GetFile(objFile.Path)
    If InStr(objFile.OpenAsTextStream.ReadAll, strSearchFor) > 0 Then
        Stop
    End If
Next

Would that do the trick for you?

Cheers,
MakeItSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top