Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.net.client.defaultConnectTimeout=1200000 -Dsun.net.client.defaultReadTimeout=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
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 -Dsun.net.client.defaultConnectTimeout= and -Dsun.net.client.defaultReadTimeout= 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.net.client.defaultConnectTimeout=300000 -Dsun.net.client.defaultReadTimeout=300000 -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.
Thanks!

vbscript
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 Next strLine = Join(strLine," ") WScript.Echo strLine Loop objFile.Closeusage:
C:\test>more file SET MYSTRING=args=-server -Xrs -Xms640m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=128m -Dsun.net.client.defaultConnect Timeout=1200000 -Dsun.net.client.defaultReadTimeout=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 -Dsun.net.client.defaultConnect Timeout=newvalue -Dsun.net.client.defaultReadTimeout=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

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!

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= :loop 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 ( set v%%i=-Dsun.net.client.defaultConnectTimeout=3600000 ) ) for /L %%i in (1 1 !N!) do ( set new=!new! !v%%i! ) > newstr.txt echo.!new!
=====================================
Helping others achieve escape felicityM2

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.

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= :loop for /f "tokens=1* delims= " %%a in ("!S!") do ( echo %%a | findstr "^-Dsun.net.client.defaultConnectTimeout">NUL if not errorlevel 1 ( SET Result=!Result! -Dsun.net.client.defaultConnectTimeout=555555 ) ELSE ( SET Result=!Result! %%a ) if "%%b" neq "" ( set S=%%b goto :loop ) ) Echo %Result%

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |