Copy Files with powershell and Robocopy without deleting

Welcome Forums General PowerShell Q&A Copy Files with powershell and Robocopy without deleting

Viewing 6 reply threads
  • Author
    Posts
    • #222639
      Participant
      Topics: 4
      Replies: 10
      Points: 30
      Rank: Member

      Hi All,

      This may have been covered in another post  but I am unable to find the correct switch or code combination, I need to compare source file location with destination location and just copy any files that are in the destination but not in the source and put them in separate location(this is to keep a copy of deleted file for a set time) and then the script would do the normal /mir backup which is the part I currently have working, my current script is below any help would be appreciated.

      
      Write-Host "############## Setting File Locations ##################"
      $Source = "C:\Users\User1\Downloads"
      $Destination = "O:\Powershell_Test_Backup\test"
      $Deleted = "O:\Powershell_Test_Backup\deleted"
      $LogFileLocation = "O:\Powershell_Test_Backup\log"
      
      $Date = get-date -format _yyyy_MM_dd_hh_mm
      $LogFile = $LogFileLocation + "\Robocopy_Log" + $Date + ".txt"
      New-Item $LogFile -type file -force |Out-Null
      
      Write-Host "############## Starting Backup #####################"
      $switches = ("/MIR", "/W:30", "/R:10", "/REG", "/NP", "/LOG:$logfile")
      & robocopy $source $Destination $switches
      
    • #223530
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      /MIR is going to delete from the destination if it doesn’t exist in the source. Drop this right away. I find it hard to believe your favorite search engine didn’t provide with a plethora of robocopy info.

      https://answers.microsoft.com/en-us/windows/forum/windows_7-files/is-using-robocopy-with-the-mir-switch-safe/ad91976f-ebb5-4ba8-88a0-532807f497f0

    • #223569
      Participant
      Topics: 4
      Replies: 10
      Points: 30
      Rank: Member

      Hi Doug,

      I think I have to reword the question with what I am trying to achieve

      1. Compare Source to Destination and move files from the Destination to another location which I will call Deleted Files (this is to keep a copy of deleted files and this is the part that I am stuck on)
      2. Backup Source to Destination (the listed script does this nicely and I will not need any help)
      3. From Deleted files remove files that are older than a certain time/date (I should be able to find this one myself I am just getting stuck with number 1. at the moment)

      Hope this makes it a bit clearer!

       

    • #223575
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      OK so you want a list of the files that should be deleted so that you can move them to a “recycle bin” for a period of time before really deleting?

    • #223593
      Participant
      Topics: 4
      Replies: 10
      Points: 30
      Rank: Member

      Yep

      I was hoping to use a different version of the script I had created and possibly change the switch so it to moves the deleted files and then continue with the mir copy that I currently have created to add changed and new files to the backup.

       

    • #223608
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      You can tell robocopy to only list files it would copy with the given command line arguments. You could parse this list and use it as input for your special copyjob. đŸ˜‰

    • #223617
      Participant
      Topics: 4
      Replies: 10
      Points: 30
      Rank: Member

      Thanks for that Olaf,

      I will check that out.

       

Viewing 6 reply threads
  • You must be logged in to reply to this topic.