# VBScript help - search/replace



## jarhead8286 (Nov 6, 2004)

I am completely new to VBScript and trying to pick it up on my own. Here's the problem I am faced with... I need to search config.nt file for "files=_some_number_" string and replace it with "files=255".

I was searching the web and stumbled across VBScript regular expressions and the code posted below. The problem is it replaces a specific value instead of reading the value after the "=" sign.

Option Explicit

Dim oFSO, oFile, oRegexp
Dim strFilename, strData
Dim strPattern, strReplace

Const ForReading = 1
Const ForWriting = 2

strFileName = "C:\Windows\System32\config.nt"
strPattern = "files=40"
strReplace = "files=255"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile(strFileName, ForReading)
strData = oFile.ReadAll
oFile.Close

Set oRegexp = New Regexp
oRegexp.Pattern = strPattern
oRegexp.Global = TRUE
oRegexp.IgnoreCase = TRUE
strData = oRegexp.Replace(strData, strReplace)

Set oFile = oFSO.OpenTextFile(strFileName, ForWriting)
oFile.Write strData
oFile.Close


----------



## Rockn (Jul 29, 2001)

The script you posted is doing what it is supposed to do. What exactly do you want it to do. I ran the script here on a standard text file and it works fine.


----------



## jarhead8286 (Nov 6, 2004)

Rockn said:


> The script you posted is doing what it is supposed to do. What exactly do you want it to do. I ran the script here on a standard text file and it works fine.


The script I posted replaces "files=40" with "files=255" so in that sense it does do what it supposed to do. I can't assume that all the PCs will have a default value of files=40.


----------



## Rockn (Jul 29, 2001)

I see! I tried wildcards, but all it does is append to the string not replace the whole string.


----------

