If you're not tied to robocopy
, you can achieve this with using for
's variable substitution:-
@for /f "tokens=*" %F in ('dir /s /b /a:-d c:\') do @(
if %~zF geq 10485760 echo %F
)
dir /s /b /a:-d c:\
gives you a recursive listing (/s
) of all non-directories (/a:-d
) under c:\
in bare format (/b
) for easier parsing.
for
loops over that listing ("tokens=*"
is needed in case you have paths with spaces in them), and let's you get the reference the file's size using its ~z
variable modifier in any sub-commands (like if
to compare against the size you want).
The @
's are to suppress echoing of the commands, and can be omitted if you've called @echo off
previously (e.g., in a batch file).
No, this isn't possible using Robocopy. Mirroring a folder path using Robocopy will delete files in the destination if they no longer exist in the source. You can ensure that you don't overwrite files with older versions but you can't retain older versions / rename during the copy process.
I wrote a command script / batch file that copies modified files, but creates the tree structure based on the date that the script is run
e.g. my tree for one particular file would be.
c:\_archive\201209\
23\
0930
Todays notes.txt
1145
Todays notes.txt
24\
1000
Todays notes.txt
I could post a sample of that script if you might find that useful.
Due to popular demand, please find below my basic backup script.
@echo off
cls
rem parse the output of the date /t command to create a date in the format yyyymmdd, and also remove the delimiter (/)
rem store the results in environment variables
for /F "tokens=1,2,3,4 delims=/ " %%i IN ('date /t') do SET Z_DATE=%%k%%j%%i
for /F "tokens=1,2,3,4 delims=/ " %%i IN ('date /t') do SET Z_YEAR=%%k
for /F "tokens=1,2,3,4 delims=/ " %%i IN ('date /t') do SET Z_MONTH=%%j
for /F "tokens=1,2,3,4 delims=/ " %%i IN ('date /t') do SET Z_DAY=%%i
echo date %Z_DATE%
echo year %Z_YEAR%
echo month %Z_MONTH%
echo day %Z_DAY%
rem parse the output of the time /t command to remove the delimeter (:)
rem store the result in an environment variable
for /f "tokens=1,2 delims=:. " %%i IN ('time /t') do SET Z_TIME=%%i%%j
echo time %Z_TIME%
rem change the colour to a nice deep green on black.
color 02
::-------------------------
@echo on
xcopy "c:\MyFiles\*.*" "C:\MyArchive\ByDate\%Z_YEAR%%Z_MONTH%\%Z_DAY%\%Z_TIME%%~p1%~n1\" /ksymhr
@if @@ERRORLEVEL==1 SET Z_BACKUP_ERROR_FLAG=1
Goto End
::---------------------------------------------------------------------------
:NotifyUser
echo.
echo An error occurred during the backup.
echo.
pause
::---------------------------------------------------------------------------
:End
echo %Z_DATE%
echo %Z_TIME%
time /t
rem Clear out the environment variables
SET Z_DATE=
SET Z_TIME=
SET Z_BACKUP_ERROR_FLAG=
if you have any questions about this script (I cribbed it quickly from my main backup script, and didn't test it thoroughly) then email me at my gmail account with the subject line 'Server Fault Backup Script query'. If you can work out my gmail address without asking then I'll be happy to help you.
Best Answer
If you don't want files changed on either the original or destination side to be overwritten in the destination combine the two options /XO /XN. New files on the original side and files deleted on the destination side will still be added to the destination folder. And, deletions on the original side will also carry over to the destination.
Using only the option /XO will allow changes made to original folder files to overwrite changed destination files. But changes in the destination will stick if there is an older file in the original folder. Those changes will be overwritten if the original side is later changed.
Robocopy has a long list of options that you may find helpful. To create a reference file run:
robocopy /? >robocopy_usage.txt