FC.EXE
将设置一个 ErrorLevel 如下:
-1 Invalid syntax (e.g. only one file passed)
0 The files are identical.
1 The files are different.
2 Cannot find at least one of the files.
您的脚本可能是(带有一些调试echo
和copy
命令注释REM
)。
@echo off
set "source=C:\source"
set "remote=C:\remote"
for /F "delims=" %%G in ('dir /B "%source%\" /A:-D') do (
>NUL 2>&1 FC /b "%source%\%%~G" "%remote%\%%~G"
if errorlevel 1 (
echo %%G files differ or remote does not exist
REM copy /B /Y "%source%\%%~G" "%remote%\%%~G"
) else (
echo %%G files match
)
)
但是,ROBOCOPY.exe
- 强大的文件和文件夹复制提供了更高级的选项,包括递归到子文件夹。
如果ROBOCOPY
由于任何原因无法使用,则上述脚本更改如下:
@ECHO OFF
SETLOCAL EnableExtensions
set "sourceMain=C:\source"
set "remoteMain=C:\remote"
call :subFolder "%sourceMain%" "%remoteMain%" "%sourceMain%"
rem traverse source subfolder structure
for /F "delims=" %%g in ('dir /B /S "%source%\" /A:D 2^>NUL') do (
call :subFolder "%sourceMain%" "%remoteMain%" "%%~g"
)
ENDLOCAL
goto :eof
:subFolder
rem adapted original script
set "sourceRoot=%~1"
set "remoteRoot=%~2"
set "source=%~3"
call set "remote=%%source:%sourceRoot%=%remoteRoot%%%" compute target folder
ECHO *** comparing "%source%" vs. "%remote%" ***
rem next command creates target folder if it does not exists yet
MD "%remote%" 2>NUL
for /F "delims=" %%G in ('dir /B "%source%\" /A:-D 2^>NUL') do (
>NUL 2>&1 FC /b "%source%\%%~G" "%remote%\%%~G"
if errorlevel 1 (
echo %%G files differ or remote does not exist
REM copy /B /Y "%source%\%%~G" "%remote%\%%~G"
) else (
echo %%G files match
)
)
goto :eof
请注意,变量编辑/替换的目标文件夹计算出于方便的原因从%%g
循环体移动到:subFolder
子例程:无需激活延迟扩展。
请注意,%%G
循环保持不变。