简体   繁体   English

递归过滤目录中的所有文件,然后将它们移动到一组新文件夹中,每个文件夹中每个目录的文件数受限制

[英]Filter all files recursively in a directory and then move them to a new set of folders each with a limit of files per directory

Sorry for the long title, just trying to be specific. 抱歉,长标题仅供参考。

Here's my scenario today (as the title alludes): 这是我今天的情况(标题暗示):

Root Dir has x amount of folders, say 20. Each folder has files and possible sub-folders. 根目录目录有x个文件夹,例如20个。每个文件夹都有文件和可能的子文件夹。 I know that I only care about the files in any folder with "Hold" in the folder name. 我知道我只关心文件夹名称中带有“ Hold”的任何文件夹中的文件。

I need to target all files, no matter the type, in the folders and subs. 我需要将所有文件类型和文件夹中的内容定位为目标。 I then need to move the target files to new folders in the root dir. 然后,我需要将目标文件移至根目录中的新文件夹。 Each folder must only house 50,000 files and then roll over to the next folder for the next 50k. 每个文件夹只能容纳50,000个文件,然后再滚动到下一个文件夹以保存下50k个文件。 That process will repeat until all the targets have been moved to their new dir. 该过程将重复进行,直到所有目标均移至新目录为止。

Here's the PS code I have so far: 这是我到目前为止的PS代码:

#SETUP
$FileDir = GCI -Path C:\TEST -Filter 'Hold*' -Directory -Recurse
$FilesinHOLD = GCI -File -Path $FileDir.FullName -Recurse
$NewDir = 'C:\TEST\Limited'
$Limit = '50000'

#Execution
$FilesinHOLD | Select-Object -First $Limit | Move-Item -Destination $NewDir

Where I'm left is looking for the mechanism to auto-create a new "Limited" folder and put 50k in there and then repeat the process. 我剩下的地方是寻找自动创建新的“受限”文件夹并将其放入50k的机制,然后重复该过程。 I was wondering if it would be possible to just render the same name simply followed by (2) and then (3) and so on automatically as the default behavior in Windows Explorer..? 我想知道是否可以仅在Windows资源管理器中将默认名称呈现为相同的名称,然后紧跟(2)然后是(3)等等。 It would look like this: 它看起来像这样:

C:\\TEST\\Limited C:\\ TEST \\有限公司

C:\\TEST\\Limited(2) C:\\ TEST \\有限公司(2)

C:\\TEST\\Limited(3) C:\\ TEST \\有限公司(3)

I would finish up by running back through and deleting the now empty "HOLD" directories from which I pulled the files from after verifying they are empty via GCI or Measure-Object. 最后,我将重新运行并删除通过GCI或Measure-Object确认为空的“ HOLD”目录,然后从中删除文件。

Thanks in advance for your input. 预先感谢您的输入。

-CE -CE

Rather than grabbing all the files then removing them from the array, I would move them as I recurse the directories, then loop on that: 与其获取所有文件然后从数组中删除它们,不如在递归目录时移动它们,然后在其上循环:

$rootDir = 'C:\TEST\Limited' 
$NewDir = $rootDir
$Limit = '50000'
$filesMoved = $Limit
$folderCnt = 1
while($filesMoved -ge $limit)
{    
    $filesMoved = GCI -Path C:\TEST -Filter 'Hold*' -Directory -Recurse | GCI -File -Recurse | Select-Object -First $Limit | Move-Item -Destination $NewDir -passthru | measure | select -expand count
    $folderCnt += 1
    $newDir = $rootDir +"($folderCnt)"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Map 递归应用到目录中的所有文件夹/文件 - Map an app to all folders/files in a directory recursively 如何重命名一批文件并将其移动到新目录 - How do I rename a batch of files and move them to a new directory 使用 ffmpeg 转换目录中的所有文件,然后使用 PowerShell 移动它们 - Convert all files in a directory using ffmpeg and then move them using PowerShell 将所有文件从所有子文件夹移动到新目的地,并按Powershell中每个组的特定文件数量进行处理 - Move all files from all subfolders to new destination with grupping them by specific number of files per group in powershell 仅将新文件移动到不同的目录 - Powershell - Move only New files to a different directory - Powershell 使用目录名称上的筛选器递归删除文件和目录 - Recursively Delete Files and Directories Using a Filter on the Directory Name 如何删除文件夹和文件目录中除最新文件以外的所有文件 - How to delete all but the most recent files in a directory of folders and files Powershell:浏览目录中的所有文件(PDF)并根据前 6 个字节中写入的内容移动它们 - Powershell: Go through all files (PDF's) in a directory and move them based on what's written in the first 6 bytes 在PowerShell中递归地将一组文件从一个目录复制到另一个目录 - Recursively copy a set of files from one directory to another in PowerShell 将文件夹中的所有文件移动到新的子目录 - Moving all files in a folder to a new child directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM