Loops for循环内的If语句(MS批处理)
我制作的以下脚本有一些问题:Loops for循环内的If语句(MS批处理),loops,batch-file,if-statement,for-loop,echo,Loops,Batch File,If Statement,For Loop,Echo,我制作的以下脚本有一些问题: @echo off Setlocal EnableDelayedExpansion set type=str set idx=0 for /f "tokens=1*" %%a in (equipment.ini) do ( if !type! == str ( set /a idx=!idx!+1 set /a es.!idx!=%%a echo !es.!idx!! set type=int
@echo off
Setlocal EnableDelayedExpansion
set type=str
set idx=0
for /f "tokens=1*" %%a in (equipment.ini) do (
if !type! == str (
set /a idx=!idx!+1
set /a es.!idx!=%%a
echo !es.!idx!!
set type=int
) else (
set /a ei.!idx!=%%a
echo !ei.!idx!!
set type=str
)
)
脚本应该从文件equipment.ini中读取并将值保存在两个不同的变量中,这两个变量在每次重复for循环时都会切换。
%es.1%将包含武器的名称,%ei.1%将包含武器的ID等等
for和if循环似乎工作正常,但回显值却不行。
从昨天晚上开始,我就一直在为沮丧而烦恼,因为我知道解决办法可能很简单,但我就是看不出来
如果有人能帮助我,我将不胜感激。
谢谢 试试这个:
@echo off
Setlocal EnableDelayedExpansion
set type=str
set idx=0
for /f "tokens=1*" %%a in (equipment.ini) do (
if !type! == str (
set /a idx=!idx!+1
set /a es.!idx!=%%a
for %%# in (!idx!) do echo !es.%%#!
set type=int
) else (
set /a ei.!idx!=%%a
for %%# in (!idx!) do echo !es.%%#!
set type=str
)
)
试一试
还有它的伴侣…试试这个
@echo off&cls
setlocal enabledelayedexpansion
set $count=1
for /f "tokens=1,2 delims= " %%a in ('type equipment.ini') do (set es!$count!=%%a
set ei!$count!=%%b
set /a $count+=1)
set e
如果您的equipment.ini具有以下格式,则该选项有效:
test 1000
test1 1001
test2 1002
test3 1003
你能举一个你的
设备的例子吗。ini
。Test 1001 Test2 1002 Test3 1003它们当然每次都是新的行,但我不能把它放在评论部分。这是有效的,但出于某种原因,只对ei有效,对于es,由于某种原因,值保持为0,即使在修复了你的示例中的微小错误(2x es)后,它仍然有效,但由于某些原因,仅对ei有效,对于es,由于某些原因,该值保持为0。您的数据test*
不是数字,因此set/a es。!idx!=%%a
将变量设置为0。丢失/a
。
test 1000
test1 1001
test2 1002
test3 1003