4

Looking to parse out the path from a specific point & then use that to populate the dynamic array.

Example:

Folder tree:
C:\Main\folder1
C:\Main\folder2\folder2-1
C:\Main\folder3\folder3-1\folder3-2

Desired result:
Array[1]=folder1
Array[2]=folder2
Array[3]=folder2\folder2-1
Array[4]=folder3
Array[5]=folder3\folder3-1\
Array[6]=folder3\folder3-1\folder3-2

This is the working code below which returns fine but in full paths:

@echo off
setlocal EnableDelayedExpansion
SET folders=C:\Main

rem Populate the array with existent files in folder
set i=0
for /r "%folders%" /d %%a in (*) do (
   set /A i+=1
   set list[!i!]=%%a
)
set foldnum=%i%

rem Display array elements
for /L %%i in (1,1,%foldnum%) do (SET array[%%i]=!list[%%i]!)

for /F "tokens=2 delims==" %%f in ('set array[') do echo %%f
halfer
  • 19,824
  • 17
  • 99
  • 186
Sid
  • 107
  • 4
  • Why do you copy the array `list[]` to `array[]` in the `for /L` loop for displaying? – aschipfl Oct 25 '16 at 19:04
  • It was a block of code I found & that was included. I see the inefficiency in that & will be edited. – Sid Oct 25 '16 at 19:21
  • 3
    Looks like you asked this question a few months ago. http://stackoverflow.com/questions/38511607/batch-recursing-through-folders-populating-an-array – Squashman Oct 25 '16 at 20:02

2 Answers2

2

You pass an absolute path to the FOR loop. But even with a relative path the FOR loop does too much and converts it to an absolute path.

The trick here is to replace the absolute path in the FOR loop.

Create a copy of the loop variable in a real variable

set AA=%%a

Then replace prefix+backslash by nothing in the list "array'

set list[!i!]=!AA:%folders%\=!

full fixed code:

@echo off
setlocal EnableDelayedExpansion
SET folders=C:\Main

rem Populate the array with existent files in folder
set i=0
for /r "%folders%" /d %%a in (*) do (
   set /A i+=1
   rem create a copy of the loop variable in a real variable
   set AA=%%a
   rem replace prefix+backslash by nothing in a the list "array"
   set list[!i!]=!AA:%folders%\=!
)
set foldnum=%i%

rem Display array elements
for /L %%i in (1,1,%foldnum%) do (SET array[%%i]=!list[%%i]!)

for /F "tokens=2 delims==" %%f in ('set array[') do echo %%f

then you get all the dirs of %folders% in a relative way.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Just tested & i am still receiving the full path I think this line may be the culprit **set BB=!AA:%folders%\=!** Could you elaborate on this please – Sid Oct 25 '16 at 19:04
  • the code above works. I have edited it to simplify it (remove the BB variable which is useless and added more explanations) – Jean-François Fabre Oct 25 '16 at 19:36
  • Got it, it was syntax issue. Removed the backslash in the BB claim and it worked. The FOLDERS variable already had a backslash in the path. Thank you – Sid Oct 25 '16 at 20:06
0

A little bit of Tom Foolery.

@echo off
setlocal EnableDelayedExpansion
SET folders=C:\Main

subst B: %folders%
B:

set i=0
for /r /d %%G in (*) do (
    set /A i+=1
    FOR /F "tokens=* delims=\" %%H IN ("%%~pnG") do set "array[!i!]=%%~H"
)
C:
subst B: /D
set foldnum=%i%

rem Display array elements
for /F "tokens=2 delims==" %%f in ('set array[') do echo %%f

pause
Squashman
  • 13,649
  • 5
  • 27
  • 36