0

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.

vtastek
  • 3
  • 3
  • 2
    Shouldn't those %rand% be !rand! in stead? And can you run it with ECHO ON to see exactly when the error message happens? Your statement 'I get empty ""' instead' isn't very clear about where in the loop(s) that happens. – Tonny Jan 06 '19 at 12:37
  • I tried that, the result is now just `rand` as 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 loop `rem 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

1 Answers1

0

There are several flaws within your code

  1. with nested for loops you'd need two levels of delayed expansion (what is possible)
  2. For every iteration of the outer for /r you rebuild/overwrite the very same file[] array from source files.
  3. 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)
  4. 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"
)
LotPings
  • 7,011
  • 1
  • 15
  • 29
  • Thanks for the answer, I will try it and see how it goes. 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. – vtastek Jan 06 '19 at 18:04
  • Better [edit](https://superuser.com/posts/1391138/edit) your question to contain that explanation. Are the number of files in source and target identical? You could then use only the array, replace the file[random] entry with the last one `file[n]` and decrement `n`. – LotPings Jan 06 '19 at 18:26
  • Done. Yes they are identical. There are wav files too but they shouldn't interfere. Thanks again for the tip. – vtastek Jan 06 '19 at 18:44
  • Try changed (and untested) answer, may already do what you want. – LotPings Jan 06 '19 at 18:45
  • Thank you very much. It is working, I can even rerun it which is a good feature. – vtastek Jan 06 '19 at 18:58