0

When I try iterating the folders with a for each approach I have no access to the current index and I've also failed to manually keep one:

@echo off

set "i=0"
set folders='dir /b /ad'
for /f "eol=: delims=" %%D in (%folders%) do (
    :: echo %%D
    echo %i%
    set /a "i+=1"
)

When I try iterating with a fori approach based on this example I can't even get it working:

@echo off
cls

set "i=0"
:SymLoop
set folders='dir /b /ad'
if defined folders[%i%] (
    echo %%folders[%i%]%%
    set /a "i+=1"
    GOTO :SymLoop
)

I'm aware of my total lack of knowledge on the topic so I'd appreciate any kind of correction and/or advice.

Jesús Cruz
  • 192
  • 2
  • 18
  • https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990 – Aacini Oct 10 '17 at 03:03

1 Answers1

0
@ECHO OFF
SETLOCAL
@echo off

set /a i=0
set folders='dir /b /ad'
for /f "eol=: delims=" %%D in (%folders%) do (
    REM echo %%D
    CALL echo %%i%%
    CALL SET "folders[%%i%%]=%%D"
    set /a i+=1

)
SET fol

ECHO ---------------------------

@echo off

set /a i=0
:SymLoop
set folders='dir /b /ad'
if defined folders[%i%] (
    CALL echo %%folders[%i%]%%
    set /a "i+=1"
    GOTO SymLoop
)


GOTO :EOF

Please refer to endless examples on SO about delayed expansion for simpler ways.

Not a good idea to use ::-comments within a (code block) as it can break the block.

set /a does not ordinarily require "quotes"

Magoo
  • 77,302
  • 8
  • 62
  • 84