Compare two directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Compare two directory

#1 Post by darioit » 25 Oct 2022 11:16

Hello,
could you help me to batch compare a directory to find missing couple of file?

Example existing couple, same name "AB12345_DEF1234_20220105" and 2nd file has another qualifier "_XYZ1234567"
C:\Data\AB12345_DEF1234_20220105.pdf
C:\Data\AB12345_DEF1234_20220105_XYZ1234567.pdf

example missing both type
C:\Data\AB56789_DEF9876_20220125.pdf Missing AB56789_DEF9876_20220125_ABC1234567.pdf
C:\Data\GH54321_LMN9876_20220115_XYZ7654321.pdf Missing GH54321_LMN9876_20220115.pdf

Sometimes is missing 1st row, sometimes 2nd and

Thank you in advance
Dario

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Compare two directory

#2 Post by aGerman » 25 Oct 2022 15:37

Provided the number of tokens separated by an underscore is always 3, and 4 for the sibling:

Code: Select all

@echo off &setlocal
pushd "C:\Data"
for /f "delims=" %%i in ('dir /a-d /b "*.pdf"^|findstr /rix "[^_]*_[^_]*_[^_]*\.pdf"') do (
  if not exist "%%~ni_*.pdf" echo lone: "%%~i"
)

for /f "delims=" %%i in ('dir /a-d /b "*.pdf"^|findstr /rix "[^_]*_[^_]*_[^_]*_[^_]*\.pdf"') do (
  for /f "tokens=1-3 delims=_" %%j in ("%%~i") do if not exist "%%j_%%k_%%l.pdf" echo lone: "%%~i"
)
popd
pause
Steffen

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Compare two directory

#3 Post by darioit » 25 Oct 2022 23:33

Perfect, works fine and very fast, I don't considered using token with delims _ in my script!

Many thanks
Dario

Post Reply