I have hundreds of folders on drive A that I want to migrate to drive B. Within the drive B root, I want the folders from drive A to be put inside folders that themselves each contain 100 folders. How can this be done in Windows?

link|improve this question

70% accept rate
Are the source folders all in the same folder (on drive A)? – techie007 Nov 29 '11 at 0:49
No, but I'm willing to accept an answer that assumes so, if it's a good one. – Keyslinger Nov 30 '11 at 3:03
feedback

1 Answer

Here's some PowerShell...

[string]$source = "C:\Source Folder"
[string]$dest = "C:\Destination Folder"

# Number of folders in each group.
[int]$foldersPerGroup = 100

# Keeps track of how many folders have been moved to the current group.
[int]$movedFolderCount = 0

# The current group.
[int]$groupNumber = 1

# While there are directories in the source directory...
while ([System.IO.Directory]::GetDirectories($source).Length -gt 0)
{
    # Create the group folder. The folder will be named "Group " plus the group number,
    # padded with zeroes to be four (4) characters long.
    [string]$destPath = (Join-Path $dest ("Group " + ([string]::Format("{0}", $groupNumber)).PadLeft(4, '0')))
    if (!(Test-Path $destPath))
    {
        New-Item -Path $destPath -Type Directory | Out-Null
    }

    # Get the path of the folder to be moved.
    [string]$folderToMove = ([System.IO.Directory]::GetDirectories($source)[0])

    # Add the name of the folder to be moved to the destination path.
    $destPath = Join-Path $destPath ([System.IO.Path]::GetFileName($folderToMove))

    # Move the source folder to its new destination.
    [System.IO.Directory]::Move($folderToMove, $destPath)

    # If we have moved the desired number of folders, reset the counter
    # and increase the group number.
    $movedFolderCount++
    if ($movedFolderCount -ge $foldersPerGroup)
    {
        $movedFolderCount = 0
        $groupNumber++
    }
}
link|improve this answer
+1 for strong typing your variables in PS. ;) – techie007 Nov 30 '11 at 13:10
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.