如何用ConstructionTime(10)搜索行;在文件中并获取分配给变量的数字? [英] How to search for line with ConstructionTime(10); in a file and get the number assigned to a variable?

查看:74
本文介绍了如何用ConstructionTime(10)搜索行;在文件中并获取分配给变量的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命令

find /i "constructiontime" file.cfg

输出行

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.

这怎么办?

推荐答案

解决方案是使用命令 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 在以%ComSpec%/ C 开头的单独命令过程中,在圆括号中指定运行命令行,并捕获所有输出以处理 STDOUT (如果可以在任何行中找到该字符串,则为 Find 的输出)。

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.

启动终止后指令流程他捕获的输出由 FOR 逐行处理。空行以及以分号开头的行将被忽略,分号是默认的行结束符。

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 将所有其他行拆分为子字符串使用作为分隔符,因为 delims =()。在这种情况下,第一个子字符串为 constructiontime ,这没有意义。第二个子字符串是 10 ,它是感兴趣的字符串。第三个子字符串将是; ,它也不重要。因此, tokens = 2 用于将第二个子字符串分配给指定的循环变量 I

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.

循环变量 I 的值分配给环境变量 ConstrutionTime 用于下一个命令行以显示在命令提示符窗口中运行此小批处理文件的结果。

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 /?

  • 查找/?

  • 用于/?

  • 如果/?

  • set /?

  • echo /?
  • find /?
  • for /?
  • if /?
  • set /?

这篇关于如何用ConstructionTime(10)搜索行;在文件中并获取分配给变量的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆