Trying to figure out how to set a maximum number of sub folders to create in a folder and then moving a specific number of files into each folder, leaving the remaining files in the original location after reaching the max folders and files per folder.
Below script works fine creating a “Sorted” subfolder and then subfolders beneath the subfolders and moving the files into it but I am having difficulty with adding the maximum number of subfolders to create under the “Sorted” subfolder…
$filesperfolder = 2
$folderMax = 3
$files = '*.txt'
$sourcePath = "Path to source files"
$destPath = "Destination Path"
$i = 0;
$folderNum = 1;
Get-ChildItem "$sourcePath\$files" | % {
while($folderNum -le $folderMax)
{
New-Item -Path ($destPath + "\" + $folderNum) -Type Directory -Force
}
Move-Item $_ ($destPath + "\" + $folderNum);
$i++;
if ($i -eq $filesperfolder){
$folderNum++;
$i = 0 ;
}
}