Thursday, November 30, 2017

powershell - Robocopy replace same date file move



I'm using robocopy to move data. I want to achieve this



IF Source File data (Newer or Same) move/overwrite data

Else skip file



I need a switch or a way to just excluded newer not newer or same data. I'm using Robocopy to clean and move a lot of data to a big storage device.



A line to delete files from the source with matching date with target would be useful too



/XO excluded files Same date or Newer files in the target
/XN excluded files older in the source and newer in the target



Some of the data are already on that source.




My issue that I want to just excluded older not same date. I want to make sure to move (and overwrite so source get clean) the data with new or same data as the source.



I'm using /XO switch



/XO : eXclude Older - if destination file exists and is the same date or newer than the source - don’t bother to overwrite it.



The issue is I also want to move the same date data not just the newer.
Is there a way to do that?


Answer





Robocopy replace same date file move



IF Source File data (Newer or Same) move/overwrite data Else skip file




To complete this use Robocopy with the example batch script I provided below sytax and all.




I want to just excluded older not same date. I want to make sure to

move (and overwrite so source get clean) the data with new or same
data as the source.



The issue is I also want to move the same date data not just the
newer.




Since by default Robocopy will only copy a file if the source and destination have different time stamps or different file sizes, you have to use the /IS switch for it to also copy the same data over too, and then if you use the /MOVE switch, it'll also remove those files and folders from the source.







Robocopy Options Used



(I used these switches in particular but you should test to confirm all works as expected just in case.)



/S         : Copy Subfolders, but not empty ones.
/NP : No Progress - don’t display % copied. (for cleaner log file)
/R:n : Number of Retries on failed copies - default is 1 million.
/MOVE : Move files and dirs (delete from source after copying).
/IS : Include Same, overwrite files even if they are already the same.

/TS : Include Source file Time Stamps in the output. (for log file)
/FP : Include Full Pathname of files in the output. (for log file)
/LOG+:file : Output status to LOG file (append to existing log).


For the below variables of:




  • SET SRCRoot="\\Server\Share\Source"

  • SET DESTRoot="\\Server\Share\Destination"




The SET SRCRoot and SET DESTRoot are the root\parent directories you'll be copying to and from at the topmost level all the way down recursively. You can set either one of these to a drive letter path (e.g. C:\Path) or you can keep as UNC path (i.e. \\server\share\folder) and it'll work either way.




  • SET LOG=C:Path\Log.txt



The SET LOG is your appended log file name and location. It can also point to a drive full path or a UNC path. This is the file you can review to get all the detail of what copied, what did not, error messages, etc. This is strictly optional but I'd suggest using it always and review as-needed for troubleshooting or confirmation.




Robocopy Batch Script Example



If you need to also copy over the file level security of the files and folder you copy to source from destination (the ACLs), please let me know as you'll probably want to include the /SEC or /COPYALL swithces.



@ECHO ON
SET SRCRoot="\\Server\Share\Source"
SET DESTRoot="\\Server\Share\Destination"
SET LOG=C:Path\Log.txt

:: --// Robocopy Options

:::: --// If you do not want a log file, remove the "/LOG+:%LOG%" below
SET OPT=/MOVE /IS /S /NP /R:5 /LOG+:%Log% /TS /FP
SET CMD=Robocopy "%SRCRoot%" "%DESTRoot%" *.* %OPT%
%CMD%
EXIT /B





Optional Robocoy Switches




(If you need any of these, let me know and I'll be glad to update my answer if you need an example including any)



/E       : Copy Subfolders, including Empty Subfolders.
/SEC : Copy files with SECurity (equivalent to /COPY:DATS).
/DCOPY:T : Copy Directory Timestamps.
/COPYALL : Copy ALL file info (equivalent to /COPY:DATSOU)






Scheduling for Unattended Automation



Refer to my answer Scheduled Tasks for the options, gotchas, etc. you'll want to select when scheduling a batch script with Task Scheduler to run as expected. Screen shots and all are provided and feel free to upvote that answer if you find it useful and worthy as such—no pressure just an additional resource you may find useful.






Further Resources and Reading





No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...