×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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

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

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close