# Windows batch script X files from one directory to another

I'm not sufficient at writing any kind of batch script so I kind of mashed up this solution from elsewhere. It does the job; copies files from a directory (and all it's subdirectories) to another directory, seemingly randomly.

However it doesn't manage to copy the complete amount of files and I'm not sure why. If I set it to 10 it returns maybe 7 or 8. Any ideas? Thanks.

@echo off
setlocal

:: Create numbered list of files in a temporary file
set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt"
dir "D:\Users\...\Music" /b /s /a-d %1 | findstr /n "^" >"%tempFile%"

:: Count the files
for /f %%N in ('type "%tempFile%" ^| find /c /v ""') do set cnt=%%N

:: Copy number random files, doesn't get them all though?
for /l %%N in (1 1 100) do call :copyRandomFile

:: Delete the temp file
del "%tempFile%"

exit /b

:copyRandomFile
set /a "randomNum=(%random% %% cnt) + 1"
for /f "tokens=1* delims=:" %%A in (
'findstr "^%randomNum%:" "%tempFile%"'
) do xcopy "%%B" "D:\Users\...\Playlists\Random"
exit /b


Edit: Sorry, to explain further: I have a folder full of music (though sorted in different sub directories based on artists etc) and I want to randomly select 100 of the files and copy them to my output folder, in order to essentially have a 100 song random playlist to transfer onto mp3 player. The code above does this,but for some reason some it doesn't copy the required amount of files.

• I don't understand what you're trying to achieve. What do you want to randomize? – Brtrnd Jul 11 '16 at 7:05
• ss64.com/nt/for.html – Dave Jul 11 '16 at 7:13
• Can you explain a bit more? – Harshal Benake Jul 11 '16 at 7:20
• Sorry, to explain further: I have a folder full of music (though sorted in different sub directories based on artists etc) and I want to randomly select 100 of the files and copy them to my output folder, in order to essentially have a 100 song random playlist to transfer onto mp3 player. The code above does this,but for some reason some it doesn't copy the required amount of files. – EndOnDeath Jul 11 '16 at 8:59
• @HarshalBenake see above – EndOnDeath Jul 11 '16 at 9:17

Does it have to be Windows batch? If not, in this thread is a Powershell solution as well as some batch code.

Powershell:

Get-ChildItem SomeFolder | Get-Random -Count $x | Copy-Item -Destination SomeOtherFolder  This assumes you run the script from the folder your files reside. Replace SomeFolder with path to your files and SomeOtherFolder with your desired destination. To explain a bit further: Get-ChildItem -Path C:\path\to\directory # Lists content of the directory # To get subfolders you have to add -Recurse # to limit this only to files add -Files Get-Random -Count$x                        # Selects x random items from previous command.
# You have to define \$x beforehand

Copy-Item -Destination C:\path\to\destination   # Copies the random selected files to


You can copy this code in a text file, save as *.ps1 an right klick run with powershell.

I might be possible that powershell throws an error like script cannot be loaded because execution of scripts is disabled on this system.

If that's the case you have to start powershell as administrator and run Set-ExecutionPolicy RemoteSigned

Note: Another drawback I found while testing is that Get-Random could get you the same item more than once. Proof

To prevent this I recommend the Get-Unique command.

• I tried running this from my documents folder (where I am storing my scripts) and I got the error: Get-ChildItem : Could not find a part of the path 'D:\...\Music'. At D:\...\Documents\makeplaylist.ps1:2 char:1 So I tried adding Set-Location D:\...Music at the start of the file but still the same error. I don't really want to have to put this ps1 file inside the music folder for it to work. – EndOnDeath Jul 11 '16 at 9:13
• Do you have spaces in your path? Either source or destination? If so, you have to put C:\path\to\folder in quatation marks ("") – humble.adm1n Jul 11 '16 at 9:26
• Important Note: I edtited my answer and changed Move-Item to Copy-Item as the first command moves the files whereas the second copies the files. Apologies for any inconvenience. – humble.adm1n Jul 11 '16 at 9:35
• I had already changed the Move to Copy as I noted that when you first wrote the answer, but thanks :P Also yes I had a space that was why. However it is still producing strange results, as with the bat script. It copied about 150 files, as well as some empty folders (artist names), rather than the 300 I specified. The error is with copy-item, saying an item with the specified name "D:\...\Plsylists\Random\Unknown Album" already exists. Note many of the files I'm trying to copy are within "...\Music\Artist\Unknown Album, though I can't see why this should affect it. – EndOnDeath Jul 11 '16 at 11:49
• Seems to work perfectly now, many thanks! I tried repping the answer but because I'm new apparently it doesn't show - thanks anyway! – EndOnDeath Jul 11 '16 at 16:11