Move a specific number of files to folders from a large pool of files

files

I have 1 million files in a single folder (source). I want to create a new folder, move 5K files into it and repeat until all files in source are moved over to separate destination folders. The end result would be 200 new folders with 5K files each. I want to do this in Windows preferably.

The folder names can be randomly created, the files can be randomly picked from the pool.

Update 1: The resulting 200 folders will live directly under root, so no destination folder structure is needed.

Best Answer

To answer what you want exactly, this will move 5,000 files into folders, starting with 1 and continuing until it runs out of files to move. Your last folder will most likely have less than 5,000 files in it.

In powershell, untested, so YMMV.

$filesperfolder = 5000
$sourcePath = "C:\Temp"
$destPath = "C:\Temp\Sorted"
$i = 0;
$folderNum = 1;

Get-ChildItem "$sourcePath\*.pdf" | % {

    New-Item -Path ($destPath + "\" + $folderNum) -Type Directory -Force
    Move-Item $_ ($destPath + "\" + $folderNum);

    $i++;

    if ($i -eq $filesperfolder){
        $folderNum++;
        $i = 0 ;
    }
}

This isn't in line with what you've asked exactly, but a common way of dealing with this issue is to break the files into two more depths, based on the first characters in the filename.

This does not distribute the files evenly, but if the files are named with GUIDs, you'll have a limit of 256 folders at each level.

In Powershell, to sort out *.pdf from c:\temp to c:\temp\sorted:

$sourcePath = "C:\Temp"
$destPath = "C:\Temp\Sorted"
Get-ChildItem "$folderPath\*.PDF" | %{
    $newFolder = $destPath + ("\" + $_.Name.SubString(0,2) + "\"  + $_.Name.SubString(3,2)).ToUpper();

    New-Item -Path $newFolder -Type Directory -Force
    Move-Item $_ $newFolder
}