I have asked this question yesterday and it was closed for syntax error, although in my tests, errorlevel was registering correctly but it is besides the matter.
My problem is, an index variable I set outside a loop, is not getting incremented inside the loop such as this example:
set idx=0
for %%f in (P_*.txt) do (
REM do something here
set /a idx=idx+1
)
echo %idx%
result is always zero, although the files matching the pattern are over 200 in the current directory.
Why ?
EDIT: As requested, a minimally reproducible example from my cmd prompt screen scrape is below
C:\test>dir *.txt
Volume in drive C has no label.
Volume Serial Number is 48E0-D051
Directory of C:\test
04/19/2020 11:58 PM 8 p_2.txt
04/20/2020 12:00 AM 7 p_3.txt
04/20/2020 12:00 AM 7 p_4.txt
04/20/2020 12:00 AM 7 p_5.txt
04/20/2020 12:00 AM 7 p_6.txt
04/20/2020 12:00 AM 7 p_7.txt
04/20/2020 12:00 AM 7 p_8.txt
04/20/2020 12:00 AM 7 p_9.txt
8 File(s) 57 bytes
0 Dir(s) 217,249,751,040 bytes free
C:\test>type 2.bat
@echo off
set idx=0
for %%f in (P_?.txt) do (
setlocal enabledelayedexpansion
echo %%f
set /a idx=idx+1
echo %idx%
echo ------------
)
C:\test>2.bat
p_2.txt
0
------------
p_3.txt
0
------------
p_4.txt
0
------------
p_5.txt
0
------------
p_6.txt
0
------------
p_7.txt
0
------------
p_8.txt
0
------------
p_9.txt
0
------------
C:\test>