I'm using the following script to archive everything prior to Jaunary 1, 2013:

foreach  ($i in Get-ChildItem "O:\Projects” -Force -Recurse)
    if ($i.LastWriteTime -gt ($(Get-Date).AddDays(-649)))
          robocopy $i.FullName "A:\Operations\Projects" /Z /S /MOVE /MIR

the problem is the script is not copying the exact directory tree. it is only copying the folder containing a pertinent file to the root of the target directory.

As an example, this would represent our folder structure:

  • Project A
    • Quote
      • QuoteA.pdf (last modified 6/2011)
    • Closeout
      • CloseoutA.pdf (last modified 11/2011)
  • Project B
    • Quote
      • QuoteB.pdf (last modified 10/2012)
    • Closeout
      • CloseoutB.pdf (last modified 4/2013)

my script above would result in the following target folder:

  • Quote
    • QuoteA.pdf
    • QuoteB.pdf
  • Closeout
    • CloseoutA.pdf

the above example is extremely basic. Some of the files are 8 or 9 folder deep. there are nearly 30,000 folders within the source directory. I need the full path of the applicable file to remain intact in the move, how can I accomplish that?


Edit from comments below:

You could try to let robocopy decide if a file in a tree needs to be moved and avoid using Get-Childitem:

robocopy "O:\Projects" "A:\Operations\Projects" /Z /S /MOVE /MIR /MINAGE:649
  • I was using $i.FullName but $i.DirectoryName did not help regardless. It was just putting all the files in the root of the target directory, with no subfolders. It's saying "ERROR 2 (0x00000002) Accessing Source Directory" for some reason. permissions are set to full for "everyone" on the entire target drive, I'm not sure else why it would encounter an error while accessing the source directory. also, I'm a little confused if I'm supposed to use -gt or -lt to select objects older than the given time. – jchap1590 Oct 15 '14 at 5:03
  • Concerning the date, you mentioned ($i.LastWriteTime -gt ($(Get-Date).AddDays(-649), this means that $i.LastWriteTime is more recent than 649 days ago. If you want older than, use -lt. – Micky Balladelli Oct 15 '14 at 6:09
  • I think Get-ChildItem is confusing Robocopy. You see, you first get a directory tree, then ask Robocopy to /MIR and /MOVE that tree, then the Foreach makes an access to the next item in the tree, but it was already moved by the previous iteration. – Micky Balladelli Oct 15 '14 at 6:30
  • I'll try the robocopy command without running it via powershell, but I thought I had read there is no way to make robocopy recursive in and of itself, thus the reason to execute via powershell. – jchap1590 Oct 15 '14 at 13:37
  • the command seems to be doing what I want! EDIT: never mind, I got it. thanks so much! – jchap1590 Oct 16 '14 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.