我是批处理新手并且一直试图让它正常工作,但无法弄清楚我做错了什么。计数器由于某种原因没有增加?
@echo off
set local enabledelayedexpansion
set /a counter=0
for /F "delims=" %%a in ('dir /b/ad/o "C:\Sources"') do (
for /F "delims=" %%i in ('dir /b/ad/o "C:\Sources\%%a"') do (
set a[%counter]=%%i
set /a counter=counter+1
echo value of counter is: %counter%
)
)
echo array 0 is: %a[0]%
echo array 1 is: %a[1]%
答案 0 :(得分:1)
SET LOCAL
应该是SETLOCAL
。这是一个单一的命令。嵌套变量也应该引用!var!
而不是%var%
。如果您使用%var%
,它将使用外部范围(并且无法正常工作)。
https://ss64.com/nt/delayedexpansion.html
@ECHO OFF
REM "SETLOCAL" not "SET LOCAL"
SETLOCAL enabledelayedexpansion
SET counter=0
FOR /L %%a IN (1,1,10) DO (
FOR /L %%i IN (1,1,10) DO (
REM USE "!" instead of "%"
SET X[!counter!]=%%i
SET /a counter=!counter! +1
echo value of counter is: !counter!
)
)
ECHO array 0 is: %X[0]%
ECHO array 1 is: %X[1]%
顺便说一句,如果你想让你的变量只限于你的批处理文件,你应该用ENDLOCAL
结束你的脚本