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.
Answer
There are several flaws within your code
- with nested for loops you'd need two levels of delayed expansion (what is possible)
- For every iteration of the outer
for /r
you rebuild/overwrite the very samefile[]
array from source files. - if you build the array first there is no need to constantly switch between source and target (the folder is stored in the array anyway)
- despite the name scrambler it's unclear what you want to achieve with two copy and one move, please elaborate.
:: Q:\Test\2019\01\06\SU_1391138.cmd
rem scrambler
@echo off
setlocal EnableDelayedExpansion
cd j:\source
set n=0
for /R %%f in (*.mp3) do (
set /A n+=1
set "file[!n!]=%%f"
)
cd j:\target
for /R %%t in (*.mp3) do (
set /A "rand=(n*!random!)/32768+1,m=n,n-=1"
rem following line demonstrates double delayedexpansion with a pseudo call
call set "file=%%file[!rand!]%%"
call set "file[!rand!]=%%file[!m!]%%"
echo copy /B /Y "!file!" "%%t"
copy /B /Y "!file!" "%%t"
)
No comments:
Post a Comment