Reading a text file, eiditing a certain line, then saving.

This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.


Thread Starter
Oct 20, 2008
Hi, I need a batch file that can read a vbs file, and edit a certain line out of it.
Example: The batch file needs to read, and search the vbs file "regearch.vbs" for the line "tomkey", then change it to, "tomkey2", then save the vbs file again.

Heres the reg file coding:
' Script that will do a substring search in all key names
' for the string in the variable sKey and delete the key
' if the string is found.
' You can set a key path in the variable sStartKeyPathx if
' you want to limit the search to a specific registry branch.
' Adjust callouts to sStartKeyPath1/2/3 as you see fit

Const OpenAsASCII      =  0
Const FailIfNotExist   =  0
Const ForReading       =  1
Const OverwriteIfExist = -1

sStartKeyPath1 =  "HKEY_CURRENT_USER"
sStartKeyPath2 =  "HKEY_LOCAL_MACHINE"
sStartKeyPath3 =  "HKEY_USERS"

sKey = "tomkey"

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

' get a temporary registry file name
sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" & oFSO.GetTempName

sStart = Now
ExportRegistry sStartKeyPath1, sTempFile
CreateAndRunRegistryFile sKey, sTempFile

ExportRegistry sStartKeyPath2, sTempFile
CreateAndRunRegistryFile sKey, sTempFile

ExportRegistry sStartKeyPath3, sTempFile
CreateAndRunRegistryFile sKey, sTempFile

' delete temp file
If oFSO.FileExists(sTempFile) Then
  oFSO.DeleteFile sTempFile
End If

finFile = "C:\finishedreg.txt"
Set completionFile = oFSO.CreateTextFile(finFile, _
OverwriteIfExist, OpenAsASCII)
completionFile.WriteLine "Finished in " & DateDiff("n", sStart, Now) & " minutes"

Sub ExportRegistry(sStartKey, sFile)
  If Trim(sKey) = "" Then
    ' export the complete registry
    sCmd = "regedit.exe /S /E:A """ & sFile & """"
    ' export the registry key to a file
    sCmd = "regedit.exe /S /E:A """ & sFile & """ " & """" & sStartKey & """"
  End If
  oShell.Run sCmd, 0, True
End Sub

Sub CreateAndRunRegistryFile(sString, sInFile)

  sOutFile = oFSO.GetSpecialFolder(2).ShortPath & "\" & oFSO.GetTempName

  ' find all string starting with [ and ends with ], and that
  ' also contains at least one bachslash in addition to the
  ' text in the variable sString
  sCmd = "%comspec% /c findstr.exe " _
       & "/i /r ""^\[.*\\.*" & sString & ".*\]$"" """ _
       & sInFile & """ >""" & sOutFile & """"

  oShell.Run sCmd, 0, True

  Set fFile = oFSO.OpenTextFile(sOutFile, ForReading, _
                                    FailIfNotExist, OpenAsASCII)

  sResult = ""
  On Error Resume Next
  sResult = fFile.ReadAll
  oFSO.DeleteFile sOutFile
  On Error Goto 0

  If sResult <> "" Then

    Set fRegFile = oFSO.CreateTextFile(sOutFile, _
                    OverwriteIfExist, OpenAsASCII)
    fRegFile.WriteLine "REGEDIT4" & vbCrLf

    aResult = Split(sResult, vbCrLf)

    For i = 0 To UBound(aResult)

      sLine = aResult(i)

      ' do the same tests as the regexp in findstr just in case.
      If InStr(1, sLine, sString, vbTextCompare) > 0 _
                  And Left(sLine, 1) = "[" And Right(sLine, 1) = "]" Then

      sLine = "[-" & Mid(sLine, 2)
        fRegFile.WriteLine sLine
      End If

    fRegFile.WriteLine vbCrLf   ' add two blank lines at the end
    sCmd = "regedit.exe /s """ & sOutFile & """"
    oShell.Run sCmd, 0, True

  End If

  If oFSO.FileExists(sOutFile) Then
    oFSO.DeleteFile sOutFile
  End If

End Sub
Any ideas?
Aug 7, 2007
Seems like a lot of work to just change one line considering everytime you want to change it you have to edit the file that does the changing, or type the filename, search and replace strings on the command line. Easier to just edit the file directly. That's the whole point of using variables, so you only need to change one line.

If you are calling this script from another file, be easier to just modify it to use a passed variable, then you don't need to edit it.
Just change this line:
sKey = "tomkey2"
to this:
sKey = Wscript.Arguments.Item(0)

Then call it like this:
cscript /nologo regearch.vbs SearchForMe

You'd want to check that a value was actually passed, or you'll get an error.

Easier to do this kind of editing with a VBScript.
Const ForReading = 1
Const ForWriting = 2
strFile = Wscript.Arguments.Item(0)
strSearch = Wscript.Arguments.Item(1)
strReplace = Wscript.Arguments.Item(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(StrFile, ForReading)
strText = objFile.ReadAll
strNewText = Replace(strText, strSearch, strReplace,1,-1,1)
Set objFile = objFSO.OpenTextFile(StrFile, ForWriting)
objFile.WriteLine strNewText
Save it as SearchandReplace.vbs
Call it like this:
cscript /nologo SearchandReplace.vbs FilenametoSearch StringtoSearchfor ReplaceWithThis

If you want it as a batch file:
@Echo Off
Set _File=C:\Temp dir\Test\Test.vbs
Set _Search=tomkey
Set _Replace=tomkey2
Set _VBTemp=%temp%\tmp.vbs
(Echo.Const ForReading = 1
Echo.Const ForWriting = 2
Echo.strFile = Wscript.Arguments.Item^(0^)
Echo.strSearch = Wscript.Arguments.Item^(1^)
Echo.strReplace = Wscript.Arguments.Item^(2^)
Echo.Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
Echo.Set objFile = objFSO.OpenTextFile^(StrFile, ForReading^)
Echo.strText = objFile.ReadAll
Echo.strNewText = Replace^(strText, strSearch, strReplace,1,-1,1^)
Echo.Set objFile = objFSO.OpenTextFile^(StrFile, ForWriting^)
Echo.objFile.WriteLine strNewText
cscript /nologo "%_VBTemp%" "%_File%" "%_Search%" "%_Replace%"
Del "%_VBTemp%"
Edit the set lines as needed, or convert it to use parameters.
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online