[英]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.