0

This question is similar to my previous question How to search for line with ConstructionTime(10); in a file and get the number assigned to a variable? which was solved by Mofi with following code:

@echo off
set "ConstrutionTime="
for /F "tokens=2 delims=()" %%I in ('%SystemRoot%\System32\find.exe /I "constructiontime" file.cfg') do set "ConstrutionTime=%%I"
if defined ConstrutionTime echo The construction time is: %ConstrutionTime%

I can get the number 10 from a line with ConstructionTime=(10); using this code from file.cfg.

But what about Effective("flyer", 100%);?
How to get 100 from it and assign it to an environment variable?

Mofi
  • 46,139
  • 17
  • 80
  • 143

2 Answers2

0

get the right tokens and delimiters (don't care about the %, it gets lost by parsing)

@echo off
set "string=Effective("flyer", 100%);"
for /f "tokens=2 delims=) " %%a in ("%string%") do set "number=%%a"
set number
Stephan
  • 53,940
  • 10
  • 58
  • 91
0

The solution working for the line example is:

@echo off
set "Effective="
for /F "tokens=2 delims=%%) " %%I in ('%SystemRoot%\System32\find.exe /I "Effective" file.cfg') do set "Effective=%%I"
if defined Effective echo The effective value is: %Effective%

This solution requires that the string in double quotes does not contain a space character nor is there a space character left to the space character left to the value of interest.

Another solution would be:

@echo off
set "Effective="
for /F "tokens=2 delims=%%)," %%I in ('%SystemRoot%\System32\find.exe /I "Effective" file.cfg') do for /F %%J in ("%%I") do set "Effective=%%J"
if defined Effective echo The effective value is: %Effective%

This solution works also for lines with no space after the comma and for lines with one or more spaces anywhere left to the number of interest. But the quoted string must not contain a comma.

A third solution would work also with multiple spaces and commas left to the number of interest.

@echo off
set "Effective="
for /F tokens^=3^ delims^=^" %%I in ('%SystemRoot%\System32\find.exe /I "Effective" file.cfg') do for /F "delims=%%), " %%J in ("%%I") do set "Effective=%%J"
if defined Effective echo The effective value is: %Effective%
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • I'd suggest the `)` is not required in the `delims` clause of the first solution – Magoo Apr 20 '19 at 08:39
  • @Magoo I know that. I added `)` just to make sure all three code variants work also if there is just a number without percent sign. I forgot to mention this in my answer. – Mofi Apr 20 '19 at 08:48