Copy Files with powershell and Robocopy without deleting

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

/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

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!

 

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?

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.

 

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. :wink:

Thanks for that Olaf,

I will check that out.