0

I need to pick a random filename from specific directory N times but how to get it when both array and index variable must be called using delayed expansion?

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET /A filesCount=0
FOR %%f IN (*) DO (
    SET files[!filesCount!]=%%~f
    SET /A filesCount+=1
)

FOR /L %%x IN (1, 1, !N!) DO (
    SET /A srcIndex=!random! %% !filesCount!
    SET srcFile=!files[!srcIndex!]!
    ECHO srcFile
)

ENDLOCAL

The problem at SET srcFile=!files[!srcIndex!]!.

Now it looks like "value of files[ then srcIndex and then value of ]"

How to make it "value of files[i] where i is value of srcIndex"?

Powercoder
  • 695
  • 1
  • 5
  • 25
  • 1
    `for %%i in (!srcIndex!) do SET srcFile=!files[%%i]!` This management is explained at [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990)... – Aacini Oct 14 '18 at 14:07
  • Possible duplicate of [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script) – Squashman Oct 14 '18 at 15:07
  • @Squashman I glad there is helpful answer for me but that question isn't actually duplicate of mine – Powercoder Oct 14 '18 at 15:15
  • @Powercoder yes it is. That is why Aacini linked to it. Trust me when saying that at this point in time just about 99.999999% of the questions asked here are duplicates. Most people don't take the time to find the duplicate or are to lazy to search for it. – Squashman Oct 14 '18 at 15:19
  • @Squashman, true if you think different problems with the same solution are the same problems. I saw that answer before asking own question but didn't found it useful due to lack of exp. Thanks to Aacini for showing what exact I was needed. There is great answer but why would I search for solution of nested delayed expansions under question about data structures and sort algorithms? – Powercoder Oct 14 '18 at 15:43
  • @Aacini, thanks, I upvoted your answer by link. – Powercoder Oct 14 '18 at 15:49
  • @Powercoder because you created an array. That is what that question and answer talks about which is why Aacini linked to it. – Squashman Oct 14 '18 at 17:03

0 Answers0