none
Script that Searches for string in text files then executes a file copy not working. RRS feed

  • General discussion

  • 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 

    • Changed type Bill_Stewart Monday, March 12, 2018 9:21 PM
    • Moved by Bill_Stewart Monday, March 12, 2018 9:21 PM This is not "fix/debug/rewrite my script for me" forum
    Monday, February 5, 2018 11:01 AM

All replies

  • Hi,

    I would suggest to write this script in PowerShell.

    Have a look at: Get-ChildItem, Get-Content, Copy-Item

    Monday, February 5, 2018 11:22 AM
  • Understand that VBScript is a legacy solution, however the environment that this will be running have powershell script execution policy disabled, and for reasons unknown it wont be enabled.
    Monday, February 5, 2018 11:31 AM
  • The PowerShell execution policy seems to be a widely misunderstood feature.

    It is really an administrative safety mechanism, not a security feature.

    It is not a security feature because you can disable it from the PowerShell command line or in the current session.


    -- Bill Stewart [Bill_Stewart]

    Monday, February 5, 2018 4:00 PM
  • I suspect that you are not getting the results that you expect in the InStr comparison.  I would add a wscript.echo statement after the compare to see if it is getting triggered. 

    One thing to remember is the comparison you are using is case sensitive.

    Monday, February 5, 2018 6:20 PM
  • Proper formatting and coding would make you failure easier to find.  Since we cannot guess at the purpose you can start with this:

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    set objShell = CreateObject("WScript.Shell")
    
    set oShellEnv = objShell.Environment("Process")
    computerName = oShellEnv("ComputerName")
    objStartFolder = "C:\Test"
    
    WScript.Echo computerName
    
    Set objFolder = objFSO.GetFolder(objStartFolder)
    
    For Each objFile in objFolder.Files
    
    	Wscript.Echo objFile.Name
    	strFile = "C:\Test\" & objFile.Name
    	set objFile = objFSO.getFile(strFile)
    	
    	fileText = oFSO.OpenTextFile(strFile).ReadAll()
    	If InStr(fileText, computerName) > 0 Then
    		objFSO.CopyFolder "C:\test\ssavers\ssavers1" , "C:\screensavers\"
    	Else
    		WScript.Echo "Not found"
    	End If
    	
    Next 
    If you must use VBScript then you will need to learn VBScript.  Changing scripts you have found using guesses will not work.


    \_(ツ)_/



    • Edited by jrv Monday, February 5, 2018 7:55 PM
    Monday, February 5, 2018 7:49 PM
  • This is the more direct way to use files from the collection.

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    set objShell = CreateObject("WScript.Shell")
    
    set oShellEnv = objShell.Environment("Process")
    computerName = oShellEnv("ComputerName")
    objStartFolder = "C:\Test"
    
    WScript.Echo computerName
    
    Set objFolder = objFSO.GetFolder(objStartFolder)
    
    For Each objFile in objFolder.Files
    
    	Wscript.Echo objFile.Name
    	
    	fileText = objFile.OpenAsTextStream.ReadAll()
    	If InStr(fileText, computerName) > 0 Then
    		objFSO.CopyFolder "C:\test\ssavers\ssavers1" , "C:\screensavers\"
    	Else
    		WScript.Echo "Not found"
    	End If
    	
    Next 


    \_(ツ)_/

    Monday, February 5, 2018 8:00 PM