0

I cracking my head for over 2 hours to solve this but i still have not understand how this works.

This code suppose to go thru all PNGs inside current script folder and put into folder1 array.

Then I need to randomly generate number inside a loop and randomly pick photos from the array and return the photo file name.

Thanks in advance.

echo 
cls
setlocal enableDelayedExpansion

set /a photos=1
for %%G in (*.png) do (set folder1[!photos!]=%%~G 
set /a photos+=1 )

set totaloutput=2

for /l %%x in (1, 1, %totaloutput%) do (

    set /a "_rand=(%RANDOM% * 20 /32768)+1"
    
    echo _rand is !_rand1!
    echo folder1 is "!folder1[%%_rand]!"
    echo folder1 is "!folder1[%_rand%]!"
    echo folder1 is %folder1[!_rand!]%
    
    )

Final code:

echo off
cls
setlocal enableDelayedExpansion

set /a photos=1
for %%G in (*.png) do (
    set folder1[!photos!]=%%~G
    set /a photos+=1 )

set totaloutput=10

for /l %%x in (1, 1, %totaloutput%) do (
    set /a "_rand=(!RANDOM! * (%photos%-1) /32768)+1"
    
    echo _rand is !_rand!
    FOR %%G IN ("!_rand!") DO echo folder1 is "!folder1[%%~G]!"
)

Output Sample:

_rand is 2
folder1 is "b2.png"
_rand is 6
folder1 is "b6.png"
_rand is 3
folder1 is "b3.png"
_rand is 3
folder1 is "b3.png"
_rand is 5
folder1 is "b5.png"
_rand is 6
folder1 is "b6.png"
_rand is 2
folder1 is "b2.png"
_rand is 3
folder1 is "b3.png"
_rand is 3
folder1 is "b3.png"
_rand is 6
folder1 is "b6.png"
mAzri
  • 23
  • 7
  • 1
    See [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/a/10167990/778560) – Aacini Sep 10 '21 at 14:41

1 Answers1

3

You have a couple of problems with your code.

You need to use delayed expansion with the RANDOM variable. Every iteration of the output will be the same unless you do. The random calculation should also use the count of your files otherwise you are limited to the first 20 files in your existing code. So that line should change to this.

set /a "_rand=(!RANDOM! * %photos% /32768)+1"

Because you are inside a parenthesized code block you essentially need to do two layers of expansion to get the value of the array. You have two options to do that.

CALL method

CALL echo folder1 is "%%folder1[!_rand!]%%"

FOR meta-variable.

FOR %%G IN ("!_rand!") DO echo folder1 is "!folder1[%%~G]!"

Edit: One more bug in your code. If there is one file in the directory your file count will be two. You need to initialize the variable to zero and add one to it before you create the array variable.

set /a photos=0
for %%G in (*.png) do (
    set /a photos+=1
    set folder1[!photos!]=%%~G
)
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • Thanks for pointing out the mistakes. I have updated the code however the randomizer sometimes pick number one more outside %photos% value. I have update the main post on this. – mAzri Sep 09 '21 at 01:37
  • revising to as follow resolve the issue though but I am not sure if there if more suitable solution set /a "_rand=(!RANDOM! * (%photos%-1) /32768)+1" – mAzri Sep 09 '21 at 02:40
  • @mAzri, the problem is not with the random code. It is with the counting of the number of files. Even if there is one file in the directory your count will be 2. You need to initialize the variable to zero. Then add one in your loop before you create the array variable. – Squashman Sep 09 '21 at 03:08