1.

Solve : How Extract Value between Parethesis?

Answer»

I need to extract the value between parentheses into a variable, I am not sure if there an easy way to do it.

STRING="QMNAME(testqm) STATUS(Running)"

How can I get the following:
var1=testqm
var2=Running

This must be done in DOS only....

ThxI am hoping you mean NT family COMMAND prompt & not real MS-DOS...

1. Batch code

Code: [SELECT]@echo off
set STRING="QMNAME(testqm) STATUS(Running)"
for /f "tokens=1-4 delims=()" %%A in (%string%) do (
set var1=%%B
set var2=%%D
)
echo var1=%var1%
echo var2=%var2%

2. Output

Code: [Select]var1=testqm
var2=Runningyou can use vbscript
Code: [Select]Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
str1 = objArgs(0)
s=Split(str1,")")
For i=LBound(s) To UBound(s)
c=InStr(s(i),"(")
If c > 0 Then
WScript.Echo Mid(s(i),c+1)
End If
Next

save as test.vbs and on command line:
Code: [Select]C:\test>cscript /nologo test.vbs "QMNAME(testqm) STATUS(Running) BLAH(blah blah) "
testqm
Running
blah blah


what tool are you using to display running PROCESS status? Quote from: et_phonehome_2 on November 09, 2009, 02:50:22 PM

I need to extract the value between parentheses into a variable, I am not sure if there an easy way to do it.

STRING="QMNAME(testqm) STATUS(Running)"

How can I get the following:
var1=testqm
var2=Running

This must be done with Batch only....

Thx

There is no doubt that the batch solution is much better than the the VBS attempt above. Plus Et_Phone, the original poster, requested Batch.

VBS needs a board of its own.

p.s.: VBS failed to assign testqm and running to variables.

Casper failed to read instructions from the original poster? Quote from: billrich on November 09, 2009, 05:29:39 PM
There is no doubt that the batch solution is much better than the the VBS attempt above. Plus Et_Phone, the original poster, requested Batch.
no, you lost soul. the vbscript gets all text between parenthesis , no matter how many. The batch only gets 2nd and 4th.

Quote
VBS needs a board of its own.
tell that to the administrator of the site.

Quote
p.s.: VBS failed to assign testqm and running to variables.
trivial trivial. you are always harping on trivial things. you sound like a woman to me.

Quote
Casper failed to read instructions from the original poster?
the OP says DOS only, which i will assume he meant cmd.exe , so until he says "i don't need a vbscript", i will still keep posting vbscript solutions.. so what you gonna do about it ? go suck a thumbSalmon Trout, thanks for the response, I never realize that with DELIM, one can specify more than one expression.

Thanks to all for your response. The reason I did not ask for VBScript, its because none of my servers have VB on it....Quote from: et_phonehome_2 on November 10, 2009, 05:48:17 AM
its because none of my servers have VB on it....
its vbscript, not VB.!! what WINDOWS server version are you running ?


Discussion

No Comment Found