Copy random files into different folders by 5 files to each folder

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
Eugene1111
New Member
New Member
Posts: 1
Joined: 2020-07-09, 13:07 UTC

Copy random files into different folders by 5 files to each folder

Post by *Eugene1111 »

Hi, people. I have a big list of files, that I was able to make in a random order (with a custom column "random number"). (I even made the list of these files in a txt list file for some reason).
But now I need to put them into.. lets see....740 files divide by 5...
into 148 new folders
ok, I can make new 148 folders with an extDir utiity

but
how can I copy each 5 files into a one of a 148 folders separately
so the 1-5 files go to the dir1
the 6-10 files go to dir2
11-15 to dir3
etc
(and yes, I tried to do it manually.. but got lost..also I need to repeat the operation with different files about ten times...)
Thank you very much:)
User avatar
Stefan2
Power Member
Power Member
Posts: 4158
Joined: 2007-09-13, 22:20 UTC
Location: Europa

PowerShell: Copy each 5 random files into new folders

Post by *Stefan2 »

Hi and welcome Eugene1111

This works for me:
(win10; PS 5.1)

FROM (example):
D:\rive\Top\FolderAxy\
D:\rive\Top\FolderBxy\
D:\rive\Top\name1.txt
D:\rive\Top\name2.txt
D:\rive\Top\name3.txt
D:\rive\Top\name4.txt
D:\rive\Top\name5.txt
D:\rive\Top\name6.txt
D:\rive\Top\name7.txt
D:\rive\Top\name8.txt
D:\rive\Top\name9.txt

TO:
D:\rive\Top\FolderAxy\
D:\rive\Top\FolderBxy\
D:\rive\Top\Fld1\name1.txt
D:\rive\Top\Fld1\name2.txt
D:\rive\Top\Fld1\name3.txt
D:\rive\Top\Fld1\name4.txt
D:\rive\Top\Fld1\name5.txt
D:\rive\Top\Fld2\name6.txt
D:\rive\Top\Fld2\name7.txt
D:\rive\Top\Fld2\name8.txt
D:\rive\Top\Fld2\name9.txt

D:\rive\Top\name1.txt
D:\rive\Top\name2.txt
D:\rive\Top\name3.txt
D:\rive\Top\name4.txt
D:\rive\Top\name5.txt
D:\rive\Top\name6.txt
D:\rive\Top\name7.txt
D:\rive\Top\name8.txt
D:\rive\Top\name9.txt

USE:

copy each five to new sub folder:

- open PoSh console in D:\rive\Top\
- paste in
GCI | ForEach{$M=5 ; $i=0 ; $F=1} {MD ("Fld$F") -EA 0 ; $i++ ; Copy $_.Name ("Fld$F") ; if($i -eq $M){$i=0 ; $F++}}
- Enter, done.

$M >>> is max files = 5
$F >>> is folder numbering
"Fld$F" >>> is string "Fld" + number in $F


But to exclude folders use:
GCI -File |ForEach ......
(remember: needs PS v5; for older versions use -not $psIsContainer)


- - -


copy each five in NaturalSort to new sub folder:

But for my test files (with numbering) the order processed was not in Natural sort order
(may be not that interesting for you if you want to copy in random order anyway):
Fld1\name1.txt
Fld1\name10.txt
Fld1\name11.txt
Fld1\name12.txt
Fld1\name13.txt
..
Fld3\name2.txt
Fld3\name18.txt
Fld3\name19.txt
Fld3\name20.txt
Fld3\name21.txt

So I had to google how to sort natural, here is a way to do that:
https://stackoverflow.com/questions/5427506/how-to-sort-by-file-name-the-same-way-windows-explorer-does
Command 1:
$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
Command 2:
GCI -File | Sort $ToNatural | ForEach .....


So at the end I used this:
$ToNatural={[regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20)})}
GCI -File|Sort $ToNatural|ForEach{$M=5;$i=0;$F=1}{MD ("Fld$F") -EA 0;$i++;Copy $_.Name ("Fld$F");if($i -eq $M){$i=0; $F++}}




- - -

Another one

copy each five in random order to new sub folder:

dir -File |Sort-Object {Get-Random}|ForEach{$M=5;$i=0;$F=1}{MD ("Folder$F") -EA 0;$i++;Copy $_.Name ("Folder$F");if($i -eq $M){$i=0; $F++}}

"Folder$F" >>> is string "Folder" + number in $F

Result:
Folder1\name2.txt
Folder1\name26.txt
Folder1\name42.txt
Folder1\name45.txt
Folder1\name49.txt 
Folder2\name31.txt
Folder2\name51.txt
Folder2\name54.txt
Folder2\name56.txt
Folder2\name57.txt

ok,ok, it's not that random, we would be in need to search for a better algorithm.



 HTH? :D



- - -

bestimmte Anzahl Dateien (hier 50) in Unterordner (hier "Ordner1 , Ordner2 ,...) verschieben:

GCI -FILE| ForEach{$ANZAHL=50 ; $idx=0 ; $UnterordnerNummer=1} {MD ("Ordner$UnterordnerNummer") -EA 0 ; $idx++ ; Copy $_.Name ("Ordner$UnterordnerNummer") ; if($idx -eq $ANZAHL){$idx=0 ; $UnterordnerNummer++} }{"* * * Alles erledigt"}



 
User avatar
solid
Power Member
Power Member
Posts: 747
Joined: 2004-08-09, 11:20 UTC

Re: Copy random files into different folders by 5 files to each folder

Post by *solid »

Sorry for late reply.
Use Mover plugin
https://totalcmd.net/plugring/mover64.html
Post Reply