simple shuffle and copy

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Grim
Posts: 2
Joined: 13 Apr 2012 03:49

simple shuffle and copy

#1 Post by Grim » 13 Apr 2012 05:11

hi guys, ... this should be an easy one
i need a way to copy files from one folder to another in random order (i am surprised my search efforts failed to find one). the purpose is to get songs onto an SD card in a shuffled order which i can change every couple of weeks, or when i add a new album. my player only reads data in the order it was copied.
- directories would be j:\music to H:\music
i would also be open to having the files renamed with a random number infront of them ie., 001-500, provided i can then copy them to another folder in the newly numbered order, and that when the program was run for a second time it would replace the numbers not append to them.

-if it can run on vista/win 7 that would be great, but i will be dual-installing xp and win 7 soon, as i will be upgrading to ssd for my main drive

(very sorry if there is a post on this already, but i spent alot of time searching before asking for help)

ty for any help

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: simple shuffle and copy

#2 Post by foxidrive » 13 Apr 2012 06:38

This will copy 100 random MP3 or WMA files from the current folder to H:\music
(not fully tested)

You will need to download GAWK.EXE for this and put it on the path.


Code: Select all

@echo off
:: Version 2.1
:: copy random MP3/WMA files

set numfiles=100
set "target=H:\Music"

set tempfile2="%temp%.\randfile.txt"
set "lines="
set n=0
dir *.wma *.mp3 /b /a:-d >%tempfile2% 2>nul
if errorlevel 1 (
echo No MP3/WMA files to play in this folder
echo.
pause
goto :eof
)

for /f "delims=" %%a in ('find /c /v "" ^<%tempfile2%') do set lines=%%a

:loop
for /f "delims=" %%c in (
'GAWK "BEGIN{srand();rand();t=int(%LINES%*rand())+1}NR==t" %tempfile2%'
) do (
if not exist "%target%\%%~nxc" (
echo copying %n% - "%%c"
copy /b "%%c" "%target%" >nul
if errorlevel 1 (
echo Error - is %target% full?
pause
goto :EOF
)
set /a n=n+1
)
)
ping -n 3 localhost >nul

if %n% LSS %numfiles% goto :loop

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: simple shuffle and copy

#3 Post by foxidrive » 13 Apr 2012 07:04

If you copied the script recently then copy it again. I made some changes.

Grim
Posts: 2
Joined: 13 Apr 2012 03:49

Re: simple shuffle and copy

#4 Post by Grim » 13 Apr 2012 08:32

Thankyou so much for your quick and helpful response

it may a little time for me to understand it properly, but it has already done all i need

Aacini
Expert
Posts: 1886
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: simple shuffle and copy

#5 Post by Aacini » 13 Apr 2012 19:47

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Get file names and create input order vector
cd /D j:\music
set n=0
set input=
for %%f in (*.mp3) do (
   set /A n+=1
   set "file[!n!]=%%f"
   set input=!input! !n!
)
set "input=%input% "

rem Sort input vector into output random order vector
set output=
for /L %%i in (%n%,-1,2) do (
   set /A "randIndex=(!random!*%%i)/32768+1"
   call :MoveInputToOutput !randIndex!
)
set output=%output%%input%

rem Copy names in random order
for %%i in (%output%) do (
    copy "!file[%%i]!" H:\music
)
goto :EOF

:MoveInputToOutput randIndex
for /F "tokens=%1" %%n in ("%input%") do (
   set output=!output! %%n
   set input=!input: %%n = !
)
exit /B

Post Reply