This code works and picks a random file but when I put it inside the outer loop, I get empty "" instead.
rem scrambler
setlocal EnableDelayedExpansion
@echo off
cd j:\target
rem for /R %%t in (*.mp3) do (
REM echo ********************
REM echo T folder is %%~dpt
cd j:\source
set n=0
for /R %%f in (*.mp3) do (
set /A n+=1
set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768+1"
rem copy "!file[%rand%]!" j:\target
echo "!file[%rand%]!"
cd j:\target
REM copy "!file[%rand%]!" %%~dpt
REM move "!file[%rand%]!" j:\old
rem )
PS. What I am trying to do is: identical source and target folders with mp3 files in them. Then I loop through the target files and for each target file, I overwrite them with a random mp3 file from the source folder. I (re)move the file from source and randomly pick another source file for the next target file, so I exhaust all files without any duplicates. In the end the target has same files and structure as source but they are now scrambled, that is if I can achieve it. I know the array takes some time so I want to optimize it too.
randas a string. It is working with the code above already, printing the random file name. Thanks for the ECHO ON tip. The error is echo"!file[%rand%]!"here. If you enable the outer looprem for /R %%t in (*.mp3) do (, you can see it.I suspect the EnableDelayedExpansion part, I should put it somewhere else I guess.
– vtastek Jan 06 '19 at 13:12