GreatMagusKyros
GreatMagusKyros

Reputation: 75

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

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.

Upvotes: 2

Views: 2798

Answers (1)

GreatMagusKyros
GreatMagusKyros

Reputation: 75

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

Upvotes: 2

Related Questions