0

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>
MelBurslan
  • 2,383
  • 4
  • 18
  • 26
  • 1
    Nothing wrong with it from what I can see. – Squashman Apr 20 '20 at 03:50
  • The snippet of code that you posted does not exhibit the issue you are experiencing. If you believe that it does, please check that the script and the `P_*.txt` files are in the same directory and that `dir P_*.txt` outputs what you are expecting. – SomethingDark Apr 20 '20 at 04:00
  • 1
    In other question, you mention `I echo the idx value in the loop it stays at zero all the time`. Inside the loop may need `set enabledelayedexpansion` to expand variables with updated values. Examples need to be [mre]. – michael_heath Apr 20 '20 at 04:42
  • @michael_heath I have added the enabledelayedexpansion into the loop and still the same result. Please see the screen capture in the original question. – MelBurslan Apr 20 '20 at 07:09
  • @SomethingDark please see the output in the original question. A subset of the files I copied to a testing directory. – MelBurslan Apr 20 '20 at 07:10
  • 1
    Thanks @MelBurslan. Move `setlocal enabledelayedexpansion` up just below `@echo off` as you do not need to set that option in the `for` loop. Change `echo %idx%` to `echo !idx!` for that variable to delay expand. Try it again. View `set /?` for more help if needed. – michael_heath Apr 20 '20 at 07:31
  • The `set /A` command works, it does increment the value; the problem is `echo %idx%` as @michael_heath already mentioned; when you put `echo %idx%` after the loop (hence after the closing `)`) the number of matching files will be returned, unless your original code appears in another loop or code block... – aschipfl Apr 20 '20 at 09:14
  • Your original code should run as intended. The code t in the EDIT section does not match the original code at all and needs [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028). – Stephan Apr 20 '20 at 09:36
  • 1
    @michael_heath Thank you for the ```!idx!``` suggestion. That worked just the way I expected and I was not aware of different indirect notation for delayed expansion. I am from the Unix side and a single $ sign at the beginning of variable is all I need and know. Thanks again. – MelBurslan Apr 21 '20 at 02:24

0 Answers0