【问题标题】:Compare two folders' contents instead of two files contents' through batch file通过批处理文件比较两个文件夹的内容而不是两个文件的内容
【发布时间】:2019-01-07 02:25:55
【问题描述】:

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

我想跟踪对此网络文件夹所做的更改,因此我保留了许多备份文件夹,它们的日期戳+时间戳名称例如 20181224145231 这是在 12 月的 24th 创建的备份文件夹( 12), 2018 14h52min31sec.

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

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

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

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

所以总结我的问题是:

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

【问题讨论】:

标签: batch-file backup


【解决方案1】:

根据你的要求,在cmets中指定,你可以试试:

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

【讨论】:

  • 这正是我所需要的......从你的代码中获取最后一个备份文件夹有点困难,因为它一直在检索像 path_to_script_file\last_backup_folder 这样的文件夹,所以我用我的交换了它工作FOR /F " tokens=*" %%i IN ('dir "%folder_to_search%" /b /ad-h /od') DO (SET "last_backup_folder=%folder_to_search%\%%i")除此之外,太棒了...非常感谢您努力提供帮助...干杯伙伴...
猜你喜欢
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
  • 2018-03-14
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多