none
how to find replace vbs script for .reg file RRS feed

  • Question

  • My wish is to create a vbs script that looks at a .reg file and does a find and replace function for "domain\username" to "contoso.com\bgates". I would achieve that by using %username% to just replace it with the user that is logged on to the machine. So far I've done some research and a bunch of trial and error to bring me to this code:

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Set objFile = objFSO.OpenTextFile("c:\scripts\File.reg", ForReading)
    strText = objFile.ReadAll
    
    objFile.Close
    
    username  = obJShell.ExpandEnvironmentStrings("%username%")  
    
    Set objFile = objFSO.OpenTextFile("c:\scripts\File.reg", ForWriting, True)
    
    objFile.Write Replace(strText,"user" , username)
    
    objFile.Close

    I'm getting an error on line 3 character 1. "Invalid procedure call or argument".

    Any help would be greatly appreciated.

    Thanks,

    Jerry

    Monday, February 12, 2018 2:21 PM

Answers

  • Don't use VBScript anymore. Use Powershell instead:
    $File = 'c:\scripts\File.reg'
    $Content = Get-Content -Path $File | 
        ForEach-Object {
            $_ -replace 'user',$ENV:USERNAME
        } 
    Set-Content -Path $File -Value $Content

    Best regards,

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Monday, February 12, 2018 2:58 PM
    • Marked as answer by SilentCow Monday, February 12, 2018 3:41 PM
    Monday, February 12, 2018 2:57 PM

All replies

  • Don't use VBScript anymore. Use Powershell instead:
    $File = 'c:\scripts\File.reg'
    $Content = Get-Content -Path $File | 
        ForEach-Object {
            $_ -replace 'user',$ENV:USERNAME
        } 
    Set-Content -Path $File -Value $Content

    Best regards,

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Monday, February 12, 2018 2:58 PM
    • Marked as answer by SilentCow Monday, February 12, 2018 3:41 PM
    Monday, February 12, 2018 2:57 PM
  • THANKS!!!! THIS IS SOOOOO EASSSSYYY
    Monday, February 12, 2018 3:41 PM