展平目录结构 [英] Flatten directory structure
问题描述
下面的函数会展平目录结构并根据上次选择的写入日期复制文件。
The below function flattens the directory structure and copies files based on the last write date chosen.
function mega-copy($srcdir,$destdir,$startdate,$enddate)
{
$files = Get-ChildItem $SrcDir -recurse | Where-Object { $_.LastWriteTime -ge "$startdate" -and $_.LastWriteTime -le "$enddate" -and $_.PSIsContainer -eq $false };
$files|foreach($_)
{
cp $_.Fullname ($destdir+$_.name) -Verbose
}
}
这在较小的目录中非常成功,但是当尝试将其用于具有多个子目录和文件计数的目录时从数十万到数千万不等,它只是停滞不前。我运行了此文件并将其放置24小时,并且没有复制任何文件,也没有在PowerShell控制台窗口中显示任何内容。在这种特定情况下,大约有2700万个文件。
This has been very successful on smaller directories, but when attempting to use it for directories with multiple sub-directories and file counts ranging from the hundreds of thousands to the tens of millions, it simply stalls. I ran this and allowed it to sit for 24 hours, and not a single file was copied, nor did anything show up in the PowerShell console window. In this particular instance, there were roughly 27 million files.
尽管速度很慢,但是简单的批处理文件可以毫无问题地完成这项工作。
However a simplistic batch file did the job with no issue whatsoever, though it was very slow.
推荐答案
简单的答案是:使用中间变量会导致文件移动启动的巨大延迟。
Simple answer is this: using the intermediate variable caused a huge delay in the initiation of the file move. Couple that with using
-and $_.PSIsContainer -eq $false
而不是简单地使用-file开关,答案是对我的脚本进行了一些简单的修改,结果是:
as opposed to simply using the -file switch, and the answer was a few simple modifications to my script resulting in this:
function mega-copy($srcdir,$destdir,$startdate,$enddate)
{
Get-ChildItem $SrcDir -recurse -File | Where-Object { $_.LastWriteTime -ge "$startdate" -and $_.LastWriteTime -le "$enddate" } | foreach($_) {
cp $_.Fullname ($destdir+$_.name) -Verbose
}
}
这篇关于展平目录结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!