locked
Getting files older than and then copying only those RRS feed

  • Question

  • Hi all

    I am trying to list all files that have not been in use for x number of days and then copy only those files from the original folder to one set by me. 

    $CutoffTime = (Get-Date).AddDays(-825)
    $files = Get-ChildItem  -Recurse  -Path "Folder Path" | Where-Object { $_.LastAccessTime -le "$CutoffTime" } | Select-Object FullName | out-string 
    foreach ($file in $files){
    
    Copy-Item -literalpath $file -Destination "destination Path"
    
    }


    i have also tried the copy job with just the -path in copy-item. 

    Each time i try this solution i get the below error:

    Copy-Item : Cannot find drive. A drive with the name '
    O' does not exist.
    At line:5 char:1
    + Copy-Item -literalpath $file -Destination (my path) ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (
    O:String) [Copy-Item], DriveNotFoundException
        + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

    For the life of me i cannot figure out what i am doing wrong. Also ideally i would like it to copy the folder structure. 

    Thank you all kindly in advance for any assistance. 

    Regards

    K


    • Edited by Kirior Tuesday, August 20, 2019 8:58 AM insert code block
    Tuesday, August 20, 2019 7:42 AM

Answers

  • but can i use it with the powershell from my first post? ... Will i simply have to substitute the copy-item with robocopy?

    You wouldn't need Powershell at all. Simply provide robocopy what you want to copy and it will do the job. Please read the COMPLETE help to know about all available options.

    Recreating the folder structure by hand is something i would ideally like to avoid,

    Of course I meant to recreate the folder structure with Powershell.  ;-) You could even recreate only the folder structure with robocopy! ;-) ;-)


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by Kirior Tuesday, August 20, 2019 12:34 PM
    Tuesday, August 20, 2019 11:38 AM

All replies

  • Please format your code as code using the code posting tool provided on the icon bar of the post editor (second to last icon). Thanks.

    $CutoffTime = (Get-Date).AddDays(-825)
    Get-ChildItem -Recurse -File -Path "folder path" | 
        Where-Object { $_.LastAccessTime -le $CutoffTime } | 
            Copy-Item -Destination "destination path"


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Tuesday, August 20, 2019 9:29 AM
    Tuesday, August 20, 2019 8:35 AM
  • Hi, sorry for that. 

    I have updated the original post with code blocks.

    Regards,

    K

    Tuesday, August 20, 2019 8:59 AM
  • Great. Thanks.

    Did you try my suggestion?


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, August 20, 2019 9:30 AM
  • Hi

    Took me a second to relalize what was different between your suggestion and my code but after trying it it did work. 

    Question following from that, is it possible to copy the folder structure as well from the source to destination? 

    Let say i scan my top level share folder for anything older than 10 years and want to move it to my backup folder but keep the structure in case someone asks for something back?

    Regards,

    K

    Tuesday, August 20, 2019 10:52 AM
  • is it possible to copy the folder structure as well from the source to destination?

    Of course. Either you copy the complete folder or you recreate the source folder structure at the destination first and then copy the files to the according folder. ;-)

    But actually you should take a look at robocopy. It is able to do most of the tasks relating file copy actions out of the box. ;-)


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Tuesday, August 20, 2019 10:58 AM
    Tuesday, August 20, 2019 10:56 AM
  • HI

    I know of robocopy and used it many times, but can i use it with the powershell from my first post? 

    Will i simply have to substitute the copy-item with robocopy?

    Recreating the folder structure by hand is something i would ideally like to avoid, and copying the whole folder might again not be an option if it contains files that have been recently accessed as well as those that have not been accessed in the last x number of years.

    If my questions are those of a thick variety, i do apologize in advance. 

    Regards,

    K

    Tuesday, August 20, 2019 11:26 AM
  • but can i use it with the powershell from my first post? ... Will i simply have to substitute the copy-item with robocopy?

    You wouldn't need Powershell at all. Simply provide robocopy what you want to copy and it will do the job. Please read the COMPLETE help to know about all available options.

    Recreating the folder structure by hand is something i would ideally like to avoid,

    Of course I meant to recreate the folder structure with Powershell.  ;-) You could even recreate only the folder structure with robocopy! ;-) ;-)


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by Kirior Tuesday, August 20, 2019 12:34 PM
    Tuesday, August 20, 2019 11:38 AM
  • Hi

    HA OMG my mind is blown. I have to confess that i have been on that Microsoft technet website many many times but usually just skimming through to find the parts and switches that i wanted but never paid full attention to everything that was available. This is what i needed, and SOOO simple. 

    I have used my power shell to verify source and destination folders and they are the same. 

    Truly you live and learn. 

    I will run more tests but this seems to be what i wanted so far. Thank you for making me read it all. 

    MIND BLOWN.

    Regards,

    K

    Tuesday, August 20, 2019 12:24 PM