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
AXISHKAsked:
Who is Participating?
 
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
Sorry again, yes. You obviously understand how it is intended to work ;-).
@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 %dst% %src% /L/D/S ^| find /v "copied"') do xcopy %%F %arch%\%%F
robocopy %src% %dst% /mir /r:1 /w:1 /dcopy:dat /MT:32
popd

Open in new window

0
 
carlos sotoIT AdministratorCommented:
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/
0
 
8046586Commented:
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.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AXISHKAuthor Commented:
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.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
 
AXISHKAuthor Commented:
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.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Correct. It can be done in a batch file to automate, but yes, you need three steps.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
 
AXISHKAuthor Commented:
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
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
 
AXISHKAuthor Commented:
'xcopy tst2 tst1',

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


Thx again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.