Como organizar pastas automaticamente em grupos de cem?

1

Eu tenho centenas de pastas na unidade A que quero migrar para a unidade B. Dentro da raiz da unidade B, eu quero que as pastas da unidade A sejam colocadas dentro de pastas que cada uma contém 100 pastas. Como isso pode ser feito no Windows?

    
por Keyslinger 29.11.2011 / 01:10

1 resposta

2

Aqui estão alguns 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++
    }
}
    
por 29.11.2011 / 02:22