0

I'm a bit stuck here, and by that I mean I'm banging my head against the wall.

I'm trying to create a script that does the following:

Checks files/folders and moves all folders/files older than one day into another folder, whilst keeping the folder structure accurate.

Currently, I have this:

Get-ChildItem -Path $SourceFolder -Recurse | 
Where-Object  {$_.LastWriteTime -lt (Get-Date).AddDays(-1)} |
    foreach {
     Move-Item  $AzCopyFolder -Force}}

and I'm really not sure what I'm even doing anymore. It works, but it also copies files inside one of the folders that aren't older than one day. Help would be appreciated.

Thanks.

  • 3
    Possible duplicate of [PowerShell script to move files and folders including subfolders from one location to another older than x days](http://stackoverflow.com/questions/14935076/powershell-script-to-move-files-and-folders-including-subfolders-from-one-locati) – henrycarteruk Apr 05 '17 at 14:46
  • I don't think it's a duplicate. Moving the folders/subfolders works just fine, it's just moving more than I want it to. It's not seeing that there's a file in one of the folders that doesn't meet the criteria. I think. Maybe. – Ethan Young Apr 05 '17 at 14:49
  • What version of powershell are you using? – Bali C Apr 05 '17 at 14:49
  • So do you mean to be working with the lastwritetime stamp? Your code is looking at both folders and files so it is very possible to be going into folder contents and matching files. robocopy usually comes up a suggestion for this. – Matt Apr 05 '17 at 15:11
  • 1
    Aha, I actually just used robocopy instead. I didn't even think of using it until I saw it on another answer! – Ethan Young Apr 05 '17 at 15:42

1 Answers1

1

So, after hours of searching and screaming internally, I just used RoboCopy.

robocopy $SourceFolder $DestFolder /e /move /minage:1

Job done. Thank god.