windowsbatch-filecmdscriptingdirectory-structure

Compare 2 directories based on their folder structure in batch


I am working on a batch script that takes user input of a source folder and a destination folder. I want my script to iterate through both folder structures and log which folders are not present in either directory. I want to ignore the files in the folder and subfolders and only focus on the what folders are present. Currently I am trying to iterate through the source folder and compare each iteration to the destination folder and log whatever iteration does not match up, then I am doing the same with the destination folder and checking if there are any extra folders in the destination that are not in the source.

here is my script:

@echo off
setLocal enabledelayedexpansion
::asking user to input the folders to compare

set /p "sourceFolder=Enter Source Folder: "
echo.
set /p "destinationFolder=Enter Destination Folder: "

echo source: %sourceFolder%
echo destination: %destinationFolder%

REM Log file to store mismatched folders
@SET compareLog=%HOMEPATH%\mismatchLog_%COMPUTERNAME%_%CDATETIME%_.log
echo Mismatched folders between %sourceFolder% and %destinationFolder% > %compareLog%

::iterate through folder and compare structure
for /r "%sourceFolder%" %%i in (*.*) do (
    set "relativePath=%%i"
    set "relativePath=!relativePath:%sourceFolder%=!"


::check if same folder exists in destination

    if not exist "%destinationFolder%!relativePath!"(
    echo Folder missing in destinationFolder: !relativePath! >> %compareLog%
    )
)

::check for extra folders in destination

for /r "%destinationFolder%" %%i in (*.*) do (
    set "relativePath=%%i"
    set "relativePath=!relativePath:%destinationFolder%=!"

    if not exist "%sourceFolder%!relativePath!" (
        echo Extra Folder in destination: !relativePath! >> %compareLog%
    )
)

echo compare done
pause

There are currently no files in both the source and destination folder as I want this script to run before I add files to the destination folder to be able to check the structure beforehand. I am getting a Syntax error on the first for loop as well which I am confused by. Any help is appreciated, I hope I have provided enough information.

EDIT: The intended task for my script is to compare the file structure of 2 directories that the user inputs the paths to. The folders and subfolders that are not present in the destination folder will be logged so one can go and check which folders are not present.

CHECK ANSWER FOR WORKING CODE THAT PERFORMS THE TASK


Solution

  • Thanks to your input I was able to solve my problem and get the result I wanted. Here is the working code.

    REM Asking user to input the folders to compare.
    
    set /p "sourceFolder=Enter Source Folder: "
    echo(
    set /p "destinationFolder=Enter Destination Folder: "
    echo(
    echo source: %sourceFolder%
    echo(
    echo destination: %destinationFolder%
    echo(
    
    REM Log file to store mismatched folders
    SET compareLog=%HOMEPATH%\missmatchLog_%COMPUTERNAME%_%CDATETIME%_WMS0.CFD.log
    echo Mismatched folders between %sourceFolder% and %destinationFolder% > %compareLog%
    
    REM robocopy lists the folders not found in the destination
    %SystemRoot%\System32\robocopy.exe /l /x /v /e /mir /np /njh /njs %sourceFolder% %destinationFolder% | %SystemRoot%\System32\findstr.exe /C:"New Dir" > %compareLog%
    echo compare done
    
    pause
    exit /b