Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
Are the source folders all in the same folder (on drive A)? –  Ƭᴇcʜιᴇ007 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

1 Answer 1

up vote 2 down vote accepted

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++
    }
}
share|improve this answer
    
+1 for strong typing your variables in PS. ;) –  Ƭᴇcʜιᴇ007 Nov 30 '11 at 13:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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