首页 > 解决方案 > 通过批处理文件比较两个文件夹的内容而不是两个文件的内容

问题描述

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

我想跟踪对此网络文件夹所做的更改,因此我保留了许多备份文件夹,它们的名称为日期戳+时间戳,例如在12 月 ( )日20181224145231 创建的备份文件夹,时间为h min sec。24122018145231

我所有的 datestamp+timestamp 备份文件夹都位于一个单独的文件夹中。

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

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

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

所以总结我的问题是:

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

标签: batch-filebackup

解决方案


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

@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%
)

推荐阅读