Copy files and folders created yesterday

This topic contains 9 replies, has 5 voices, and was last updated by  copy1214 6 months, 1 week ago.

  • Author
  • #83612


    Hey All,

    I(absolute beginner in powershell) have been trying to create powershell script powershell script to copy files and folders created a day before to another drive. The source is a some what complicated structure which has several folders and files under one main folder. The files and folders get created every day. Objective is to copy all the files and folders created a day before under the main folder to the mainbackup folder. I would like to run this script everyday to keep the main and the mainbackup folder in sync. So far I have come until here:

    $source = "\\main\"
    Get-ChildItem -Path $source |
    Where-Object {$_.creationtime -f (Get-date).Addhours(-24)} |
    Copy-Item -destination $dest -Container -Recurse

    This script works but it takes for ever and goes through every file and folder in the source and throws "already exists" message. Is there a way to only to look only for files created a day before or 24 hours before, and copy them to destination? Any advice is appreciated.

  • #83615

    Michael Orellana

    I have not tested this too much but I believe you want to use '-le' instead of '-f'. I have never seen/used '-f' before but you may just know something I do not.

    Get-ChildItem -Path $source | Where-Object {$_.creationtime -le (Get-date).Addhours(-24)}
    • #83680


      Hi Michael,

      Thanks for the input. I have tried the -le and -ge. But they seem to do the same thing. The script still goes through all the folders and not just the one created in the past 24 hours and gives "already exists" message.

  • #83633


    That -f is a format attribute not a filter.

    • #83675


      Hi there,

      Thanks for the input. I am a newbie in powershell..good to know -f has been doing nothing lol :).

  • #83642

    Alex Aymonier


    Why don't you use Robocopy instead with the /a

    /A : Copy only files with the Archive attribute set.

    you can incorporate that into your ps script

    • #83677


      Hi Alex,

      Thanks for the input. I started the script with robocopy but gave up and moved on to copy-item. Haven't used the /a and going to give it a try. It seems that robocopy just doesn't handle so many files and folders. It appears that it just copies the newly created folders and not the files in those folders.

  • #83687


    Here's a robocopy backup script I have. I had to throttle the speed for some reason:

    robocopy "folder" \\server\folder /ipg:10 /fft /e /log:backup.log /tee /np /XD "\OFFICE14" "\PASWStatistics" /xf *.msu /ndl /purge
    • #84256


      Thanks Js.

      Can you give a short breakdown of the what each switch does please?

  • #83702

    Alex Aymonier

    Hey Copy,

    Check out for all the switches

You must be logged in to reply to this topic.