tempo3010
tempo3010

Reputation: 13

Batch file randomly pick files that are same file names from different sub folder to one folder

I need to randomly pick around 200 specific files from different sub folders to one folder. Following is the details: (tree /F /A result)

+--- source_folder
|   +--- Folder A
|   |      module_01.x
|   |      module_02.x
|   |      module_03.x
|   +--- Folder B
|   |      module_01.x
|   |      module_02.x
|   |      module_03.x
|   +--- Folder C
|   |      module_01.x
|   |      module_02.x
|   |      module_03.x

I want to randomly pick 3 of them for example:

module_01.x from folder B
module_02.x from folder C
module_03.x from folder C

to "dest_folder".

Following is the code I am using but only shuttles a few and it seems always picking many files from only one folder and, when it trying to copy from another folder it won't overwrite the exist files(tried the /Y but no luck).

Any suggestions? Thank you very much!

@echo off&setlocal enabledelayedexpansion
set "source_folder=test"
set "dest_folder=output"
set /a filecount=193

del "dest_folder\*.x"
pushd "source_folder"
for /r %%i in (*.x) do set /a files+=1& set "$!files!=%%~i"
popd
pushd "dest_folder"
:randomloop
set /a rd=%random%%%files+1
set "sco=!$%rd%!"
if not defined x goto :randomloop
set "$%rd%="
for %%i in ("%x%") do if exist "%%~nxi" echo "%%~nxi" already exist in %out%.& goto:randomloop
copy "%x%" /Y
set /a filecount-=1
if %filecount% gtr 0 goto:randomloop
popd

pause

Upvotes: 1

Views: 86

Answers (1)

Aacini
Aacini

Reputation: 67216

I don't understand the way you process the files... If all subfolders have the same files (as you specified in "the details"), then you don't need to process all files, but just the files from one subfolder and then, randomly select the real source subfolder...

@echo off
setlocal EnableDelayedExpansion

set "source_folder=test"
set "dest_folder=output"

del "%dest_folder%\*.x"
pushd "%source_folder%"

rem Create the subFolder array
set "n=0"
for /D %%a in (*) do (
   set /A n+=1
   set "subFolder[!n!]=%%a"
)

rem Process all files in first subfolder
pushd "%subFolder[1]%"
for %%a in (*.x) do (

   rem Select a random subfolder
   set /A "ran=!random! %% n + 1"

   rem Pick the file from such random subfolder
   for /F %%r in ("!ran!") do copy "..\!subFolder[%%r]!\%%a" "..\%dest_folder%"

)

EDIT: Simple test added

I tested this code in this way: I created the three folders and added four *.x files to each of them. Then, I added an ECHO command before the copy one, that is:

for /F %%r in ("!ran!") do ECHO copy "..\!subFolder[%%r]!\%%a" "..\%dest_folder%"

Finally I ran the program. This is the output:

copy "..\folder A\module-001.x" "..\output"
copy "..\folder C\module-002.x" "..\output"
copy "..\folder B\module-003.x" "..\output"
copy "..\folder C\module-004.x" "..\output"

This means that module-001.x file is copied from folder A, module-002.x file from folder C, module-003.x file from folder B, and module-004.x file from folder C.

Upvotes: 1

Related Questions