title: Windows batch file to list all duplicates (and the original file) in tree and sort them
body: <p>I have to check a tree for duplicating files and write all of them to List.txt file.
  But my script seems to skip one of the file locations in each group. (For example, if there are 4 duplicating files, only 3 of them appear in the list.)</p>
  
  <p>If I'm not mistaken, it's the location of the "previousFile" of the last comparison that is missing. How do I write it to the list, too?</p>
  
  <p>Also, how can I group paths in the List.txt by the filename so that it looks something like this:</p>
  
  <p>File fileNameA.txt : <br>
  C:\path1\fileNameA.txt<br>
  C:\path2\fileNameA.txt<br>
  C:\path3\fileNameA.txt<br></p>
  
  <p>File fileNameB.txt : <br>
  C:\path1\fileNameB.txt<br>
  C:\path2\fileNameB.txt<br>
  C:\path3\fileNameB.txt<br>
  C:\path4\fileNameB.txt<br></p>
  
  <p>File fileNameC.txt : <br>
  C:\path1\fileNameC.txt<br>
  C:\path2\fileNameC.txt<br></p>
  
  <p>...</p>
  
  <p>?</p>
  
  <p>That's my script so far: </p>
  
  <pre><code>@echo off
  
  setlocal disableDelayedExpansion
  
  set root=%1
  
  IF EXIST List.txt del /F List.txt
  
  set "prevTest=none"
  set "prevFile=none"
  
  for /f "tokens=1-3 delims=:" %%A in (
    '"(for /r "%root%" %%F in (*) do @echo %%~zF:%%~fF:)|sort"'
  ) do (
    set "currentTest=%%A"
    set "currentFile=%%B:%%C"
    setlocal enableDelayedExpansion
    set "match="
    if !currentTest! equ !previousTest! fc /b "!previousFile!" "!currentFile!" &gt;nul &amp;&amp; set match=1
    if defined match (
      echo File "!currentFile!" &gt;&gt; List.txt
      endlocal
    ) else (
      endlocal
      set "previousTest=%%A"
      set "previousFile=%%B:%%C"
    )
  )
  </code></pre>