Solved How to copy 6 random files from 100+ folders?

October 14, 2013 at 08:19:06
Specs: Windows 7, i5 / 8
Hello, i have more than 100 folders, and 15-20 files in each of them. And i want to copy 6 random files from each folder, and copy them somewhere else. Is it even possible to make a script like this? Thanks

See More: How to copy 6 random files from 100+ folders?

Report •

✔ Best Answer
October 20, 2013 at 03:12:41
tested but not polished

:: get 6 random files from each folder
::
::
::
::====== script starts here ===============
::
:: jelly6.bat 2013-10-20 16:32:37.60
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in ('dir/b/s/ad D:\files') do (
pushd "%%a"
call :sub1
popd
) >> NEWFILE
goto :eof

:sub1
echo ==================================
echo.!CD!
set COUNT=
for /f "tokens=* delims= " %%i in ('dir/b/a-d') do (
set/a COUNT+=1
)
if !COUNT! lss 6 goto :eof
set/a RANGE=COUNT-5
echo COUNT !COUNT!
echo RANGE !RANGE!
set/a POINTER=!RANDOM!%%RANGE
echo POINTER !POINTER!

set N=

if !POINTER! equ 0 (
for /f "tokens=* delims= " %%i in ('dir/b/a-d') do (
set/a N+=1 & if !N! gtr 6 goto :eof
echo copy %%i %TMP%\
)
)

if !POINTER! neq 0 (
for /f "tokens=* skip=%POINTER% delims= " %%i in ('dir/b/a-d') do (
set/a N+=1 & if !N! gtr 6 goto :eof
echo copy %%i %TMP%\)
)

goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle



#1
October 14, 2013 at 12:58:29
Care to tell us why you want to do this, it seems a strange request?

Always pop back and let us know the outcome - thanks

message edited by Derek


Report •

#2
October 14, 2013 at 18:14:23
Theoretically you would have to set it to pick 6 files with a random function while looping though 100 folders.

my thinking on this is that it will be time intensive to whatever system you are working with for sure.

what I would start with is a way to pull a directory of random files into a list, and then process through the list.

Still I agree with Derek.

::mike

http://www.computing.net/howtos/sho...


Report •

#3
October 14, 2013 at 18:55:36
I on the other hand am far more apathetic outside of an excuse to work on my PowerShell. Note that I don't handle possible name collisions, so that will be left as an exercise for the readers. Yes, the script starts with a comma.
, (Get-Item .) + (Get-ChildItem -Recurse | Where-Object { $_.PSIsContainer }) |
ForEach-Object { 
  $fileList = @{} 
  $files = Get-ChildItem $_.FullName | Where-Object { !$_.PSIsContainer }
  $files | Where-Object {$_} | ForEach-Object { 
    $fileList.Add($_.FullName, (Get-Random -Max $files.Count)) 
  }
  $fileList.GetEnumerator() | 
    Sort-Object Value | 
    Select -First 6 |
    ForEach-Object { Copy-Item $_.Name 'C:\somewhere else' }
}

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

Related Solutions

#4
October 15, 2013 at 01:08:09
:: This gets the "first 6" in "DOS order".
:: [don't ask]
::
::
::
::====== script starts here ===============
::
:: jelly.bat 2013-10-15 14:52:14.70
@echo off & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in ('dir/b/s/ad d:\files') do (
pushd %%a
call :sub1
popd
)
goto :eof

:sub1
set N=
echo !CD!
for /f "tokens=* delims= " %%i in ('dir/b/a-d') do (
set/a N+=1
if !N! equ 7 goto :done1
echo copy %%i somewhere
)
:done1
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#5
October 15, 2013 at 18:52:37
@echo off & setlocal enabledelayedexpansion
set dest=wherEVer
set ext=MP3
for /f "tokens=*" %%z in ('dir /b /a:d') do (
pushd %%z
for /f %%a in ('dir *.%ext% ^|find " File(s) "') do set max=%%a
attrib +a *.%ext%
for /L %%I in (1,1,6) do (
set /a e="!random! %% max"
set c=0
for /f "tokens=*" %%a in ('dir /b /a:a *.%ext%') do (
set /a c+=1
if !c! equ !e! (
:: test mode only. remove "echo" from foll line to copy the file.
echo copy %%a %dest%
attrib -a %%a
set /a max-=1
)
)
)
popd
)
::=========== end batchscript
@Derek: has all the earmarks of a CD track randomizer, would be my guess, for compiling random music for those long road trips.

@M2: yeh, no really cool way to get the count of files in a directory using batch unassisted (someone correct me if there is).
Attrib seems like the easiest (read: laziest) method for avoiding repeats of random-sampling. It also incorporates a means of avoiding repeats between "sessions".

message edited by nbrane


Report •

#6
October 16, 2013 at 00:15:16
Yep,

but FIND & ATTRIB will slow things down.

=====================
M2 Golden-Triangle


Report •

#7
October 18, 2013 at 10:05:10
I'm more disappointed that nbrane didn't continue the trend of posting a solution in a different language. I guess I'll pick up the slack and offer a VBScript version.

Set fso = CreateObject("Scripting.FileSystemObject")
Randomize
RandomCopy fso.GetFolder(".")
WScript.Echo "Done"
WScript.Quit 

Sub RandomCopy(oDir)
  On Error Resume Next
  Set fileList = CreateObject("System.Collections.ArrayList")
  For Each file In oDir.Files
    fileList.Add(file)
  Next 'file
  If fileList.Count < 6 Then
    cnt = fileList.Count
  Else
    cnt = 6
  End If
  If Err Then Exit Sub
  
  For i = 1 To cnt
    rndFile = Int(Rnd * fileList.Count)
    fso.CopyFile fileList(rndFile), "C:\somewhere else\", True
    fileList.RemoveAt(rndFile)
  Next 'i
  
  For Each dir In oDir.SubFolders
    RandomCopy dir
  Next 'dir
End Sub

How To Ask Questions The Smart Way


Report •

#8
October 20, 2013 at 03:12:41
✔ Best Answer
tested but not polished

:: get 6 random files from each folder
::
::
::
::====== script starts here ===============
::
:: jelly6.bat 2013-10-20 16:32:37.60
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in ('dir/b/s/ad D:\files') do (
pushd "%%a"
call :sub1
popd
) >> NEWFILE
goto :eof

:sub1
echo ==================================
echo.!CD!
set COUNT=
for /f "tokens=* delims= " %%i in ('dir/b/a-d') do (
set/a COUNT+=1
)
if !COUNT! lss 6 goto :eof
set/a RANGE=COUNT-5
echo COUNT !COUNT!
echo RANGE !RANGE!
set/a POINTER=!RANDOM!%%RANGE
echo POINTER !POINTER!

set N=

if !POINTER! equ 0 (
for /f "tokens=* delims= " %%i in ('dir/b/a-d') do (
set/a N+=1 & if !N! gtr 6 goto :eof
echo copy %%i %TMP%\
)
)

if !POINTER! neq 0 (
for /f "tokens=* skip=%POINTER% delims= " %%i in ('dir/b/a-d') do (
set/a N+=1 & if !N! gtr 6 goto :eof
echo copy %%i %TMP%\)
)

goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#9
October 20, 2013 at 19:50:48
Yeh, mine was defective (allowing possibility of duplicated randoms).
I was totally unfamiliar with the objects Razor referred to, so if I had ventured vbscript, I would only have embarassed myself. But, that's why I still lurk here, to learn stuff. The OP is history anyway...
On that note, with my disclaimer in place, here's (yet another of my) sloppy stab at batch:
@echo off & setlocal enabledelayedexpansion
for /f %%a in ('dir /b /a-d *.*') do (
set /a c+=1
set !c!=
)

set i=0
:k
set /a k="%random% %% c"
if defined !k! goto :k
set %k%=%k%
echo %i%: %k%
set /a i+=1
if %i% lss 6 goto :k

set c=0
for /f %%a in ('dir /b /a-d *.*') do (
if defined !c! (
echo copying #!c!: %%a
set !c!=
)
set /a c+=1
)

message edited by nbrane


Report •

#10
October 21, 2013 at 05:35:24
Yeah, I'm about to call it. He also asked on Super User, and failed to engage that community as well. Who wants the Best Answer? By my count we have 6 eligible posts.

How To Ask Questions The Smart Way


Report •

#11
October 21, 2013 at 10:47:07
Seems very suspicious to me.

Always pop back and let us know the outcome - thanks


Report •

Ask Question