copy files between dates

I'm running the following script to copy files between different dates:

$StartDate = (get-date -year 2015 -month 2 -day 6)
$EndDate = (get-date -year 2015 -month 2 -day 10)
Get-ChildItem '\\server\Restore\Pollfile_Archive\' -Recurse | Where-Object {($_.LastWriteTime -gt $StartDate.Date) -and ($_.LastWriteTime -lt $EndDate.Date)} | Copy-Item -Destination 'E:\Test\Pollfile_Archive\'

The 'Pollfile_Archive' folder has hundreds of subfolders millions of small files.
Is not copying the structure and the files.
LVL 23
Stelian StanIT Services ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
The easiest solution to your request is to just use Robocopy with the /maxage and /minage switches.
RobSampsonCommented:
Here is a Powershell way that should do it for as well.

Regards,

Rob.

$StartDate = (get-date -year 2015 -month 2 -day 6)
$EndDate = (get-date -year 2015 -month 2 -day 10)
$InputFolder = '\\server\Restore\Pollfile_Archive\'
$OutputFolder = 'E:\Test\Pollfile_Archive\'
Get-ChildItem $InputFolder -Recurse | Where-Object {$_.LastWriteTime -gt $StartDate -and $_.LastWriteTime -lt $EndDate -and $_.PSIsContainer -eq $false} | ForEach {
    $OldPath = $_.FullName
    $NewPath = "$OutputFolder" + $OldPath -replace [regex]::Escape($InputFolder), ''
    $Folder = Split-Path $NewPath -Parent
    If ((Test-Path -Path $Folder) -eq $false) {New-Item -Path (Split-Path $NewPath -Parent) -ItemType Directory -Force | Out-Null }
    Copy-Item -Path $OldPath -Destination $NewPath
}

Open in new window

Stelian StanIT Services ManagerAuthor Commented:
Sorry footech, forget to mention. I used robocopy and it worked but I just want to use powershell.

Thanks Rob. I will try it tomorrow and let you know.
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

Stelian StanIT Services ManagerAuthor Commented:
It worked. Just a minor thing. After the copy process was done only the files had the timestamp but the folders don't, the folders have today's date. Is any way that I can keep the timestamp of the folders also?

Thanks Rob.
RobSampsonCommented:
OK, I think this should do that.

$StartDate = (get-date -year 2015 -month 2 -day 6)
$EndDate = (get-date -year 2015 -month 2 -day 10)
$InputFolder = '\\server\Restore\Pollfile_Archive\'
$OutputFolder = 'E:\Test\Pollfile_Archive\'

Get-ChildItem $InputFolder -Recurse | Where-Object {$_.LastWriteTime -gt $StartDate -and $_.LastWriteTime -lt $EndDate} | Select LastWriteTime,FullName,@{n='IsContainer';e={$_.PSIsContainer}} | Sort -property @{Expression="IsContainer";Descending=$True}, @{Expression="FullName";Descending=$False} | ForEach {
    $OldPath = $_.FullName
    $NewPath = "$OutputFolder" + $OldPath -replace [regex]::Escape($InputFolder), ''
    If ($_.IsContainer -eq $True) {
        $origLastWriteTime = $_.LastWriteTime
        Copy-Item -Path $OldPath -Destination $NewPath -Force -Container
        (Get-Item $NewPath).LastWriteTime = $origLastWriteTime
    } Else {
        Copy-Item -Path $OldPath -Destination $NewPath
    }
}

Open in new window

Stelian StanIT Services ManagerAuthor Commented:
This time it didn't copy anything. Here are some of the errors I got:

Copy-Item : Could not find a part of the path 'E:\Temp\496BACKOFFICE\SALES_150206\POLL04.DOS'.
At D:\Scripts\AD\Copy-SelectedDates.ps1:14 char:9
+         Copy-Item -Path $OldPath -Destination $NewPath
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : System.IO.DirectoryNotFoundException,Microsoft.PowerShell.Commands.C
   opyItemCommand

Copy-Item : Could not find a part of the path 'E:\Temp\496BACKOFFICE\SALES_150206\POLL05.DOS'.
At D:\Scripts\AD\Copy-SelectedDates.ps1:14 char:9
+         Copy-Item -Path $OldPath -Destination $NewPath
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : System.IO.DirectoryNotFoundException,Microsoft.PowerShell.Commands.C
   opyItemCommand

Copy-Item : Could not find a part of the path 'E:\Temp\496BACKOFFICE\SALES_150206\POLL07.DOS'.
At D:\Scripts\AD\Copy-SelectedDates.ps1:14 char:9
+         Copy-Item -Path $OldPath -Destination $NewPath
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : System.IO.DirectoryNotFoundException,Microsoft.PowerShell.Commands.C
   opyItemCommand

Copy-Item : Could not find a part of the path 'E:\Temp\496BACKOFFICE\SALES_150207\POLL04.DOS'.
At D:\Scripts\AD\Copy-SelectedDates.ps1:14 char:9
+         Copy-Item -Path $OldPath -Destination $NewPath
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : System.IO.DirectoryNotFoundException,Microsoft.PowerShell.Commands.C
   opyItemCommand

But i got one for each file.
RobSampsonCommented:
Ah sorry....can you try this?

$StartDate = (get-date -year 2015 -month 2 -day 6)
$EndDate = (get-date -year 2015 -month 2 -day 10)
$InputFolder = '\\server\Restore\Pollfile_Archive\'
$OutputFolder = 'E:\Test\Pollfile_Archive\'

$arrFolderDates = @()
Get-ChildItem $InputFolder -Recurse | Where-Object {$_.LastWriteTime -gt $StartDate -and $_.LastWriteTime -lt $EndDate -and $_.PSIsContainer -eq $false} | ForEach {
    $OldPath = $_.FullName
    $NewPath = "$OutputFolder" + $OldPath -replace [regex]::Escape($InputFolder), ''
    $OldFolder = Split-Path $OldPath -Parent
    $NewFolder = Split-Path $NewPath -Parent
    If ((Test-Path -Path $NewFolder) -eq $false) {
        $origLastWriteTime = (Get-Item $OldFolder).LastWriteTime
        New-Item -Path $NewFolder -ItemType Directory -Force | Out-Null
        $FolderDates = [ordered]@{
            'Path'=$NewFolder
            'Date'=$origLastWriteTime
            }
        $arrFolderDates += New-Object PsObject -Property $FolderDates
    }
    Copy-Item -Path $OldPath -Destination $NewPath
}
$arrFolderDates | Sort Path | ForEach {
    $Path = $_.Path
    $Date = $_.Date
    (Get-Item "$Path").LastWriteTime = $Date
}

Open in new window


Regards,

Rob.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Stelian StanIT Services ManagerAuthor Commented:
It didn't copy the timestamp of the folder but is good this way too. I have the time stamp of the files which is the most important for me.

Great work Rob.

Thanks,
Stelian
RobSampsonCommented:
It may still be setting the folder "Date Modified" value. In Windows Explorer, I had to add that field to the displayed columns, after scratching my head wondering why the "Date" field wasn't being set.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.