如何自动将文件夹组织成一百个组?

如何自动将文件夹组织成一百个组?

我想要将驱动器 A 上的数百个文件夹迁移到驱动器 B。在驱动器 B 根目录中,我想要将驱动器 A 中的文件夹放入每个文件夹包含 100 个文件夹的文件夹中。如何在 Windows 中完成此操作?

答案1

这里有一些 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++
    }
}

相关内容