Robust File and Folder Copy.
By defaultRobocopy
will only copy a file if the source and destination have different time stamps or different file sizes.
robocopy C:\test1 C:\test2 /u /s
aumentaria ERROR : Invalid Parameter #3 : "/u"
.
Você pode usar o próximo snippet de código
@ECHO OFF
SETLOCAL EnableExtensions
set "source=C:\test1"
set "target=C:\test2"
for /d %%i in ("%source%\*") do (
for /d %%j in ("%target%\*") do (
if "%%~nxi"=="%%~nxj" (
robocopy "%source%\%%~nxi" "%target%\%%~nxi" * /s /e
) else (
echo "%%~nxi" Notsame! "%%~nxj"
)
rem please do not apply load global network ping 130.11.160.2
timeout /T 10
)
)
No entanto, você não precisa repetir a pasta de destino (leia IF - Condicionalmente execute um comando ) e aplique if exist
:
@ECHO OFF
SETLOCAL EnableExtensions
set "source=C:\test1"
set "target=C:\test2"
for /d %%i in ("%source%\*") do (
if exist "%target%\%%~nxi\*" (
robocopy "%source%\%%~nxi" "%target%\%%~nxi" * /s /e
) else (
echo "%%~nxi" Notsame!
)
rem please do not apply load global network ping 130.11.160.2
timeout /T 10
)
Observe a diferença (teste a existência de arquivos e pastas):
rem test a folder existence
if exist "%target%\%%~nxi\*" (echo folder exists) else (echo no such folder)
rem test a folder OR file existence
if exist "%target%\%%~nxi" (echo file OR folder exists) else (echo no such file/folder)
rem test a file (NOT folder) existence would be a bit more complex
rem (off current question topic) do it yourself in case of such demand