1

The command

find /i "constructiontime" file.cfg

outputs the line

constructiontime(10);

I want to read in this line and get just the number assigned to a variable independent on what is the number value.

How can this be done?

Mofi
  • 46,139
  • 17
  • 80
  • 143

1 Answers1

0

The solution is using command FOR:

@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%

FOR runs the command line as specified in round brackets in a separate command process started with %ComSpec% /C in background and captures everything output to handle STDOUT of this command process which is the output of FIND if the string could be found in any line.

After termination of started command process the captured output is processed by FOR line by line. Empty lines are ignored as well as lines starting with a semicolon which is the default end of line character.

FOR splits all other lines into substrings using ( and ) as delimiters because of delims=(). The first substring is in this case constructiontime which is of no interest. The second substring is 10 which is the string of interest. The third substring would be ; which is also not of interest. For that reason tokens=2 is used to assign the second substring to specified loop variable I.

The value of loop variable I is assigned to environment variable ConstrutionTime which is used on next command line to show the result on running this small batch file from within a command prompt window.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • find /?
  • for /?
  • if /?
  • set /?
Mofi
  • 46,139
  • 17
  • 80
  • 143