Copy files and folders created yesterday

This topic contains 9 replies, has 5 voices, and was last updated by  copy1214 5 days, 19 hours ago.

  • Author
    Posts
  • #83612

    copy1214
    Participant

    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\"
    $dest="\\mainbackup"
    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
    Participant

    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

      copy1214
      Participant

      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

    postanote
    Participant

    That -f is a format attribute not a filter.
    'technet.microsoft.com/en-us/library/ee692801.aspx
    'technet.microsoft.com/en-us/library/ee692795.aspx'

    • #83675

      copy1214
      Participant

      Hi there,

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

  • #83642

    Alex Aymonier
    Participant

    Hey,

    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

      copy1214
      Participant

      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

    js
    Participant

    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 gallery.zip *.msu gallery.zip /ndl /purge
    
    • #84256

      copy1214
      Participant

      Thanks Js.

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

  • #83702

    Alex Aymonier
    Participant

    Hey Copy,

    Check out https://ss64.com/nt/robocopy.html for all the switches

You must be logged in to reply to this topic.