Eduardo Ventura
Eduardo Ventura

Reputation: 13

Compare two folders' contents instead of two files contents' through batch file

I'm writing backup batch file for a particular folder that is located in a shared network folder, to my personal Windows machine.

I want to keep track of changes made on this network folder, so I keep a lot of backup folders which have a datestamp+timestamp name like 20181224145231 which is the backup folder created on the 24th of December (12), 2018 at 14h52min31sec.

All of my backup folders of datestamp+timestamp are located in a separate folder.

To do this I came up with a script that grabs date and time from the system and checks if a particular file in the original folder is different than the one located in the last backup folder using fc and a for loop to grab the last backup folder created in the past.

Things have grown and I need to compare the content of the whole folder (with subfolders) and not just a file. And that's where I've hit a wall.

I've looked into comp and fc, but can't seem to find a way. Robocopy syncs folders, but I want to create a new folder each time changes have occured. One thing I'm thinking about is creating a comparison file in both folders like a 7-zip file and running fc on both of these, but it seems rather extreme.

So summing up my question is:

How to check if the most recent backup has the same files as with the network shared folder without third party-tools through batch file?

Upvotes: 0

Views: 655

Answers (1)

double-beep
double-beep

Reputation: 5504

According to your requirements, specified in comments, you can try:

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

Upvotes: 1

Related Questions