How to search for line with Effective(“flyer”, 100%); in a file and get the number assigned to a variable?

牧云@^-^@ 提交于 2019-12-11 15:05:35

问题


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?


回答1:


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%



回答2:


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


来源:https://stackoverflow.com/questions/55769862/how-to-search-for-line-with-effectiveflyer-100-in-a-file-and-get-the-numb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!