我有一个 700 MB 的文件夹,其中包含大小各异的 24k 文本文件。我想创建每个大小约为 50 MB 的新文件夹,以便我可以使用 vba 在 excel 中处理它们,因为它的行数限制为 10 万行。
任何帮助将不胜感激。
答案1
我也对此很感兴趣,所以这里是我使用的 PowerShell 代码。这将获取主文件夹的所有文件,并将它们移动到 10MB 子文件夹中。请随意调整值和名称以满足您的需要。
# Replace with your base folder's path
$baseFolder = "C:\Users\you\myHugeFolder"
# Replace with the desired value for the subfolders to create
$maxSubFolderSize = 10MB
# Get all files contained in your base folder (could use the -recurse switch if needed)
$allFiles = Get-ChildItem $baseFolder
# Setting the subfolders naming convention : a name and a suffix
$baseSubFolder = "SubFolder-"
[int]$index = 0
# Creating the first subfolder
$subFolder = "SubFolder-" + "$index"
New-Item -Path $subFolder -Type Directory -Force
# Now processing the files
foreach ($file in $allFiles)
{
# Evaluating the size of the current subfolder
$subFolderSize = ((Get-ChildItem $subFolder -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB)
# If the current subfolder size is greater than the limit, create a new subfolder and begin to copy files in it
if([int]$subFolderSize -gt [int]$maxSubFolderSize/1MB)
{
$index++
$subFolder = $baseSubFolder + $index
New-Item -Path $subFolder -Type Directory -Force
Write-Verbose -Message "Created folder $subFolder"
Move-Item $file.FullName -Destination $subFolder
}
# If the current subfolder is not yet greater that the limit, continue copying files in it
else {
Move-Item $file.FullName -Destination $subFolder
}
}
希望这可以帮助 !