Replace substrstring within a variable string

Microsoft Windows xp professional editio...
September 21, 2009 at 12:36:55
Specs: Windows XP
Hi All,

I am relatively new with batch scripting and I wonder if someone can help me out with a challenge.

I am trying to parse a text file and replace a parameter value within one of its lines. I have been able to store the line in a variable with FOR /F, but I can't seem to find the way to replace the specific string on it.

To make a long story short, let's assume that I have:

SET MYSTRING=args=-server -Xrs -Xms640m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=128m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 "-Djava.endorsed.dirs=C:\jboss-4.0.5\lib\endorsed" -classpath "C:\Program Files\Java\jdk1.6.0_02\lib\tools.jar" -classpath "C:\jboss-4.0.5\bin\run.jar" org.jboss.Main -c

My objective is to change the 1200000 value (which can be practically any value) in the string. Length substrings are not an option in this case, since the string might be of variable length and one or more parameters may be missing. I need to identify the specific and parameters and then assign a custom value to them. Then the string should be replicated to the original format only with the parameter values changed. Something like:

MYSTRING="args=-server -Xrs -Xms640m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=128m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 "-Djava.endorsed.dirs=C:\jboss-4.0.5\lib\endorsed" -classpath "C:\Program Files\Java\jdk1.6.0_02\lib\tools.jar" -classpath "C:\jboss-4.0.5\bin\run.jar" org.jboss.Main -c"

I have been researching for some time to find a way, but no success.

I am aware that a batch file is far from being the best way to do this, but it's the only tool I can use for this purpose.

Any ideas will be very much appreciated.


See More: Replace substrstring within a variable string

Report •

September 21, 2009 at 17:51:15

Set objFS=CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file"
strNewValue = "newvalue"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
	strLine = Split(objFile.ReadLine," ")
	For i=0 To UBound(strLine)		
		If InStr(strLine(i),"client.defaultConnectTimeout")>0 Or _
			InStr(strLine(i) , "client.defaultReadTimeout")> 0 Then 
			s = Split(strLine(i),"=")
			s(UBound(s)) = strNewValue
			s = Join(s,"=")
			strLine(i) = s
		End If 
	strLine = Join(strLine," ")
	WScript.Echo strLine


C:\test>more file
SET MYSTRING=args=-server -Xrs -Xms640m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=128m
Timeout=1200000 -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server
.gcInterval=3600000 "-Djava.endorsed.dirs=C:\jboss-4.0.5\lib\endorsed" -classpath "C:\Program Files\Java\jdk1.6.0_02\lib
\tools.jar" -classpath "C:\jboss-4.0.5\bin\run.jar" org.jboss.Main -c

C:\test>cscript /nologo test.vbs
SET MYSTRING=args=-server -Xrs -Xms640m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=128m
Timeout=newvalue -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.serv
er.gcInterval=3600000 "-Djava.endorsed.dirs=C:\jboss-4.0.5\lib\endorsed" -classpath "C:\Program Files\Java\jdk1.6.0_02\l
ib\tools.jar" -classpath "C:\jboss-4.0.5\bin\run.jar" org.jboss.Main -c

GNU win32 packages | Gawk

Report •

September 21, 2009 at 23:36:57
Hi ghostdog,

Thanks much for the reply. I am not familiar with vbscript'ing at all, but I'll look into it. Do you have any link that can help to get me started on this?

Anyone else has an idea on how I can get this done with a batch script?

Thanks again!

Report •

September 22, 2009 at 01:03:32
>>Do you have any link that can help to get me started on this?

see here

GNU win32 packages | Gawk

Report •

Related Solutions

September 22, 2009 at 01:59:21
This starts with mystr.txt and produces newstr.txt. You may need to file off a few rough edges.


:: lesson learned:  echo !v%%i!
:: sammy.bat  Tue 22-09-2009 14:46:29.65

@echo off & setLocal enableDELAYedexpansion

set /p S=<mystr.txt
set N=

for /f "tokens=1* delims= " %%a in ("!S!") do (
  set /a N+=1
  set v!N!=%%a

    if "%%b" neq "" (
      set S=%%b
      goto :loop

for /L %%i in (1 1 !N!) do (
  echo !v%%i! | find "defaultConnectTimeout" > nul
    if not errorlevel 1 (

for /L %%i in (1 1 !N!) do (
  set new=!new! !v%%i!

> newstr.txt echo.!new!

Helping others achieve escape felicity


Report •

September 24, 2009 at 00:49:33
Thanks a lot Mechanix. I haven't got the time to look into this, but as soon as I test it I will post the results.

Report •

September 25, 2009 at 00:28:49
Works beautifully.
Thank you very much.

Report •

September 25, 2009 at 09:14:18
Now that I looked into it further I found a more concise and perhaps readable way based on Mechanix's idea, in case it helps anyone:

@echo off & setLocal enableDELAYedexpansion
set /p S=<mystr.txt
SET Result=

for /f "tokens=1* delims= " %%a in ("!S!") do (
  echo %%a | findstr "^">NUL  
  if not errorlevel 1 (
  SET Result=!Result!
  ) ELSE (
  SET Result=!Result! %%a
if "%%b" neq "" (
  set S=%%b
  goto :loop

Echo %Result%

Report •

Ask Question