- 1 year ago
Batch compare 2 directories and output names of folders from 2nd directory
Comment
1 / 1
To compare two directories in a batch script and output the names of folders from the second directory, you can use a for
loop and the dir
command. Here's an example script:
batch@echo off
set "dir1=C:\path\to\first\directory"
set "dir2=C:\path\to\second\directory"
for /D %%A in ("%dir2%\*") do (
if exist "%dir1%\%%~nxA\" (
echo %%~nxA
)
)
In this script:
- Modify the
dir1
anddir2
variables to the paths of the two directories you want to compare. - The
for /D
loop iterates over the folders (/D
) in the second directory (%dir2%
). - The
if exist
statement checks if the corresponding folder with the same name exists in the first directory (%dir1%\%%~nxA\
). - If the folder exists in the first directory, the name of the folder (
%%~nxA
) is echoed.
Save the script with a .bat
extension and run it to compare the directories and output the names of folders from the second directory that exist in the first directory.
Please note that this script compares folder names only and does not perform a deep comparison of folder contents. If you need to compare the contents of the folders as well, you would need to modify the script accordingly, using additional commands or tools like xcopy
or robocopy
.