如何为文件中的每个搜索结果设置不同的变量?

How to set different variables for each result of a search in a file?

这是我之前问题的补充:

我目前的代码是:

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

此代码从第 Effective("flyer", 100%); 行获取编号 100file.cfg.

但是file.cfg中有两行flyer怎么办?

我已经试过了,这段代码采用了最后一个传单值 50

Effective("flyer", 100%);

Effective("flyer", 50%);

如何选择?

包含第一张传单的第一个代码块是:

CreateWeaponType("jda.weapon.aatower")
{
  Style("Projectile");

  MaxRange(96);

  HorizAngle(90);
  HorizSeparation(180);
  VertAngle(0);
  VertSeparation(120);
  TurnRate(90);
  Speed(100);
  Fixed(1);


  Damage()
  {
    Amount(10);

    Effective("infantry", 0%);
    Effective("vehicle", 0%);
    Effective("structure", 0%);
    Effective("flyer", 100%);

    Effective("mine", 0%);
  }

  Projectile("jda.proj.aatower");
  Delay(1);
  FirePoints()
  {
    Add("HP-Fire1");
    Add("HP-Fire2");
    Add("HP-Fire3");
    Add("HP-Fire4");
  }
}

第二张传单在:

CreateObjectType("jda.exp.aatower", "Explosion")
{
  MapObj()
  {
    Mesh();
    GenericFX()
    {
      Add("ExplosionObj::Explode", "jda.fx.aatower.exp");
    }
  }
  ExplosionObj()
  {
    Damage()
    {
      Amount(16);

      Effective("infantry", 0%);
      Effective("vehicle", 0%);
      Effective("structure", 0%);
      Effective("flyer", 50%);
      Effective("mine", 0%);
    }
    AreaInner(4);
    AreaOuter(4);
    Persist(0);
  }
}

如何为每个变量设置不同的变量?

@echo off
setlocal enabledelayedexpansion
set /a count=0
for /F "tokens=2 delims=%%) " %%I in ('%SystemRoot%\System32\find.exe /I "flyer"^<file.cfg') do set /a count+=1&set "Effective[!count!]=%%I"
echo %count% items found
if %count% gtr 0 set effective[

这里的关键行是调用 delayed expansionsetlocal enabledelayedexpansion 行,其中语法 !var! 导致 var 的值,因为它可能会在 for循环。

因此,由于 count0 开始,如果检测到适当的行,则 count 递增,然后有效 [count[=35 的值=]] 设置为适当的值。

for 循环结束后,显示计数(显然,这一行是可选的)。

最后的 set 将只显示环境中 set 开头的值 effective[ - 但前提是 count 大于0.

(未经测试)

这是一个例子:

@For /F "Tokens=1*" %%A In ('FindStr /I "Effective(\"flyer\"," "file.cfg"^|Find /V /N ""')Do @For /F "Tokens=2Delims=,%% " %%C In ("%%B")Do @Set "flyer%%A=%%C"
@Set flyer[
@Pause

包含最后两行只是为了向您展示变量及其各自的值字符串。

您也可以将顶部的长行分成三行,以帮助保持合理的行长度以提高可读性:

@For /F "Tokens=1*" %%A In ('FindStr /I "Effective(\"flyer\"," "file.cfg"^
 ^|Find /V /N ""')Do @For /F "Tokens=2Delims=,%% " %%C In ("%%B"
)Do @Set "flyer%%A=%%C")
@Set flyer[
@Pause

请注意,此答案无法确定哪个百分比属于 .cfg 文件的哪个特定部分:

Magoo and Compo贴出来的答案已经是很精彩的解决方案了。所以我没有什么可以贡献的了,除非你只对第一个 flyer 值感兴趣。在这种情况下,在将 file.cfg 中的第一个 flyer 数字分配给环境变量 Effective 之后,可以使用对原始代码的小修改来退出循环并跳转到循环下方的标签.

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

find.exe 输出空行和 ---------- FILE.CFG 行,这是使用 FOR 选项 skip=2 跳过第一个的原因两行以避免将 FILE.CFG 分配给环境变量 Effective.

当然也可以重写整个代码只对第一个 flyer 值感兴趣。

@echo off
for /F "tokens=2 delims=%%) " %%I in ('%SystemRoot%\System32\findstr.exe /R /I "effective.*flyer" file.cfg') do set "Effective=%%I" & goto HaveValue
echo No line with "flyer" found in "file.cfg"
goto :EOF

:HaveValue
echo The effective value is: %Effective%

此代码使用 findstr.exe 而不是不输出 header 行的 find.exe。现在使用了一个非常简单的正则表达式搜索字符串,而不仅仅是文字搜索字符串 flyer.

但是 MagooCompo 提供的批处理文件已经很好地满足了对 flyer 这两个数字的真正兴趣。