我正在开发一个批处理脚本,该脚本接受用户输入的源文件夹和目标文件夹。我希望我的脚本能够遍历两个文件夹结构并记录哪些文件夹不存在于两个目录中。我想忽略文件夹和子文件夹中的文件,只关注存在的文件夹。目前,我正在尝试迭代源文件夹,并将每次迭代与目标文件夹进行比较,并记录任何不匹配的迭代,然后我对目标文件夹执行相同的操作,并检查目标中是否有任何额外的文件夹来源中没有。
这是我的脚本:
@echo off
setLocal enabledelayedexpansion
::asking user to input the folders to compare
set /p "sourceFolder=Enter Source Folder: "
echo.
set /p "destinationFolder=Enter Destination Folder: "
echo source: %sourceFolder%
echo destination: %destinationFolder%
REM Log file to store mismatched folders
@SET compareLog=%HOMEPATH%\mismatchLog_%COMPUTERNAME%_%CDATETIME%_.log
echo Mismatched folders between %sourceFolder% and %destinationFolder% > %compareLog%
::iterate through folder and compare structure
for /r "%sourceFolder%" %%i in (*.*) do (
set "relativePath=%%i"
set "relativePath=!relativePath:%sourceFolder%=!"
::check if same folder exists in destination
if not exist "%destinationFolder%!relativePath!"(
echo Folder missing in destinationFolder: !relativePath! >> %compareLog%
)
)
::check for extra folders in destination
for /r "%destinationFolder%" %%i in (*.*) do (
set "relativePath=%%i"
set "relativePath=!relativePath:%destinationFolder%=!"
if not exist "%sourceFolder%!relativePath!" (
echo Extra Folder in destination: !relativePath! >> %compareLog%
)
)
echo compare done
pause
当前源文件夹和目标文件夹中都没有文件,因为我希望在将文件添加到目标文件夹之前运行此脚本,以便能够事先检查结构。我在第一个 for 循环上也遇到语法错误,我对此感到困惑。感谢任何帮助,我希望我提供了足够的信息。
感谢您的输入,我能够解决我的问题并得到我想要的结果。这是工作代码。
::asking user to input the folders to compare
set /p "sourceFolder=Enter Source Folder: "
echo.
set /p "destinationFolder=Enter Destination Folder: "
echo.
echo source: %sourceFolder%
echo.
echo destination: %destinationFolder%
echo.
REM Log file to store mismatched folders
SET compareLog=%HOMEPATH%\missmatchLog_%COMPUTERNAME%_%CDATETIME%_WMS0.CFD.log
echo Mismatched folders between %sourceFolder% and %destinationFolder% > %compareLog%
::robocopy lists the folders not found in the destination
robocopy /l /x /v /e /mir /np /njh /njs %sourceFolder% %destinationFolder% | findstr /C:"New Dir" > %compareLog%
echo compare done
pause
exit /b