这是对我上一个问题的补充:
到目前为止,我的代码是:
@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%这段代码从100中的Effective("flyer", 100%);行取数字file.cfg。
但是,如果flyer在file.cfg中有两行,怎么办?
我已经尝试过了,这段代码接受了最后一个传单值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);
}
}如何为每个变量设置不同的变量?
发布于 2019-04-20 14:06:45
下面是一个例子:
@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文件的哪个特定部分:
发布于 2019-04-20 13:52:38
@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[这里的关键行是setlocal enabledelayedexpansion行,它调用delayed expansion,其中语法!var!导致var的值,因为它可能在for循环中更改。
因此,当count从0开始时,如果检测到适当的行,则count将增加,然后计数的有效值被设置为适当的值。
在for循环完成后,将显示计数(显然,这一行是可选的)。
最后的set将只显示环境中以effective[开头的值的值(S)--但只有在count大于0的情况下才能显示。
(未经测试)
发布于 2019-04-20 18:21:45
Magoo和Compo发布的答案已经是很棒的解决方案了。因此,我没有更多的贡献,除非您只对第一个flyer值感兴趣。在这种情况下,可以使用对原始代码的小修改来退出循环,在将第一个flyer号从file.cfg分配给环境变量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而不是不输出标头行的find.exe。现在使用的是一个非常简单的正则表达式搜索字符串,而不仅仅是文字搜索字符串flyer。
但是Magoo和Compo提供的批处理文件对两个flyer编号都很感兴趣。
https://stackoverflow.com/questions/55773309
复制相似问题