Link to home
Start Free TrialLog in
Avatar of AXISHK
AXISHK

asked on

robocopy

With robocopy, is it possible to move any files from the destination to another folder whenever the source file has been removed ?

1. Any files / folders in source will be synchronously exactly with that in destination.
2. Files / folders that are removed from the source will be moved to another folder

Thx
Avatar of carlos soto
carlos soto
Flag of Sweden image

hi

if you want to destination folder to be exaclty as the source folder (when adding and deleting files on source folder) you can use the /MIR flag

a little bit about /MIR flag
https://blogs.technet.microsoft.com/filecab/2008/07/31/robocopy-mir-switch-mirroring-file-permissions/
Moving the files and folders with a robocopy batch will work but to get that in the real time you have to discover which event file change will create in the event logs and you can use that event number to trigger for the batch file through task scheduler.
Avatar of AXISHK
AXISHK

ASKER

ROBOCOPY /Mir <Source> <Target>

Any idea how to add another criteria to a archive folder to includes files and folders have been removed from the source ?

Thx.
You would have ro run a /MIR with log only to get the EXTRA files (those are only on destination), then move or copy those, and then perform the real mirroring.
Avatar of AXISHK

ASKER

Do you mean I need to perform two different operation,

1. move the files that doesn't exist in the source
2. copy any file/folder changes from source to destination

I can't combine these #1 & #2 in a single command, correct ?

Thx.
Correct. It can be done in a batch file to automate, but yes, you need three steps.
Slightly changed procedere:
@echo off
set src=C:\Temp\EE\src
set dst=C:\Temp\EE\dst
set arch=C:\Temp\EE\archive

pushd %dst%
for /F "delims=" %%F in ('xcopy tst2 tst1 /L/D/S ^| find /v "kopiert"') do xcopy %%F %arch%\%%F
robocopy %src% %dst% /mir /r:1 /w:1 /dcopy:dat /MT:32
popd

Open in new window

xcopy returns a list which is processable much better. The switches say:
  /L   only report, do not copy
  /D  existing as newer file or not existing yet - the former might never happen, the second is what we are after
  /S   with subfolders

We are first changing the working directory to the destination folder, so xcopy only reports path names going forward from there. That way we are able to just append the path to the "archive" folder path. Otherwise we would have to remove path portions and do some more manipulation to get the correct archive path.

After having copied over every single file no longer existing in the source, robocopy may apply its magic - it is just the command as you would use it anyway to get an exact mirror.
Avatar of AXISHK

ASKER

Thx, what does the following do ?  

for /F "delims=" %%F in ('xcopy tst2 tst1 /L/D/S ^| find /v "kopiert"') do xcopy %%F %arch%\%%F
Good that you ask that, because I forgot to change something ;-). "kopiert" needs to be "copied".

The command runs a xcopy (as explained above), removes any line with "copied" from its output, leaving only the file and path names (of the files not being in the source location anymore).
for /f processes the results of commands (here the filtered xcopy output), and parses each line to find delimiters (space by default). The "delims=" clause prevents any splitting of the line, so we have the complete line in the for variable %%F (which then containst path and file name).
The command after do is then executed for each line.
Overall the command performs a xcopy of files no longer in source.
Avatar of AXISHK

ASKER

'xcopy tst2 tst1',

What's tst2 and tst1 respectively ? Should this be replaced with xcopy %dst% %src% ?


Thx again.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial