通过批处理文件比较两个文件夹的内容而不是两个文件的内容



我正在将位于共享网络文件夹中的特定文件夹的备份批处理文件写入我的个人Windows计算机。

我想跟踪在此网络文件夹上所做的更改,因此我保留了许多具有日期戳+时间戳名称的备份文件夹,例如20181224145231这是在 12 月24日 (12) 创建的备份文件夹,201814小时52分钟31秒。

我所有的日期戳+时间戳备份文件夹都位于一个单独的文件夹中。

为此,我想出了一个脚本,该脚本从系统中获取日期和时间,并使用fcfor循环检查原始文件夹中的特定文件是否与位于最后一个备份文件夹中的文件不同,以获取过去创建的最后一个备份文件夹。

事情已经发展,我需要比较整个文件夹(带有子文件夹)的内容,而不仅仅是一个文件。这就是我碰壁的地方。

我已经调查了compfc,但似乎找不到方法。Robocopy同步文件夹,但我想在每次发生更改时创建一个新文件夹。我正在考虑的一件事是在两个文件夹中创建一个比较文件,例如 7-zip 文件并在这两个文件夹上运行fc,但这似乎相当极端。

所以总结一下我的问题:

如何通过批处理文件检查最新的备份是否具有与没有第三方工具的网络共享文件夹相同的文件?

根据您的要求,在注释中指定,您可以尝试:

@echo off
rem Set variables for size count:
set total_size_backup=0
set total_size_origin=0
rem Find the most recent BACKUP folder:
for /F "delims=" %%A IN ('dir /b /AD /OD') do set "folder_to_search=%%~fA"
rem Find the size of all files inside the backup folder:
for /R "%folder_to_search%" %%B IN (*.*) do (
set /a "total_size_backup+=%%~zB"
)
rem Find the size of the original folder:
for /R "full_path_to_folder_with_original_files" %%C IN (*.*) do (
set /a "total_size_origin+=%%~zC"
)
rem Compare the two sizes from these two folders. If they are NOT the same include your code there.
if %total_size_backup% EQU %total_size_origin% (
echo Well Done! Your newest backup files and your original files are up-to-date!
pause>nul
exit /b 0
) else (
echo Ooops! Your newest backup files and your original files are out-of-date! Never worry! Running backup now, please wait...
start /min /wait your_backup_file.bat
echo Updated files successfully!
exit /b %errorlevel%
)

最新更新