2

I am searching for file named file.txt and want to store the path of these files in a variable which should be in dynamic nature. I am using arrays but not able to increment the value of i

@echo on
D:
cd D:\study\
setlocal enableDelayedExpansion

set /a i=1
for /r %%G in (find.*txt) do (
    set /a i=%i%+1
    echo %i%
    set obj[%i%]=%%G
    )
Arun AV
  • 86
  • 4
  • See here for examples of how to use arrays in Batch: `https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script` – T3RR0R Feb 15 '20 at 07:59
  • Type `for /?` and `set /?` and read the delayed expansion topic. Arrays don't exist in batch. Arrays in other languages are memory efficient - but every change of a variable in the environment block forces a full sort of it. All you are doing is replacing with inefficiency the **efficient** `for` construct. Plus other lines are illegal. But your approach is wrong so that doesn't matter. –  Feb 15 '20 at 08:06

1 Answers1

0

You had enabled delayed expansion, but weren't using it.

The variables you're delaying the expansion of should be referenced with exclamation marks, not percent characters.

For example:

@Echo Off
CD /D "D:\study" 2>NUL || Exit /B 1

SetLocal EnableDelayedExpansion
Set "i=0"
For /F "Delims=" %%G in ('Dir /B/S/A-D "find.txt" 2>NUL') Do (
    Set /A i +=1
    Echo !i!
    Set "obj[!i!]=%%G"
)
Compo
  • 36,585
  • 5
  • 27
  • 39