通过批处理文件比较两个文件夹的内容而不是两个文件的内容
Compare two folders' contents instead of two files contents' through batch file
我正在将位于共享网络文件夹中的特定文件夹的备份批处理文件写入我的个人 Windows 计算机。
我想跟踪在此网络文件夹上所做的更改,因此我保留了很多备份文件夹,这些文件夹具有日期戳+时间戳名称,例如 20181224145231
,这是在 [=11] 上创建的备份文件夹=]12 月 (12
),2018
在 14
h52
min31
sec.
我所有的 datestamp+timestamp 备份文件夹都位于一个单独的文件夹中。
为此,我想出了一个脚本,该脚本从系统中获取日期和时间,并使用 fc
检查原始文件夹中的特定文件是否与位于上一个备份文件夹中的文件不同,并且for
循环以获取过去创建的最后一个备份文件夹。
事情变多了,我需要比较整个文件夹(包括子文件夹)的内容,而不仅仅是一个文件。这就是我碰壁的地方。
我已经查看了 comp
和 fc
,但似乎找不到方法。 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%
)
我正在将位于共享网络文件夹中的特定文件夹的备份批处理文件写入我的个人 Windows 计算机。
我想跟踪在此网络文件夹上所做的更改,因此我保留了很多备份文件夹,这些文件夹具有日期戳+时间戳名称,例如 20181224145231
,这是在 [=11] 上创建的备份文件夹=]12 月 (12
),2018
在 14
h52
min31
sec.
我所有的 datestamp+timestamp 备份文件夹都位于一个单独的文件夹中。
为此,我想出了一个脚本,该脚本从系统中获取日期和时间,并使用 fc
检查原始文件夹中的特定文件是否与位于上一个备份文件夹中的文件不同,并且for
循环以获取过去创建的最后一个备份文件夹。
事情变多了,我需要比较整个文件夹(包括子文件夹)的内容,而不仅仅是一个文件。这就是我碰壁的地方。
我已经查看了 comp
和 fc
,但似乎找不到方法。 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%
)