Flatten directory structure

点点圈 提交于 2020-01-05 04:22:06

问题


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
    }
}

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.


回答1:


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

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
}
}


来源:https://stackoverflow.com/questions/39729491/flatten-directory-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!