title: Finding Duplicate files in a specific directory and eliminating them - Batch
body: <p>How could we find duplicated files in a certain folder and its sub-folders with also the possibility to eliminate them at the user's will?</p>
  <p>Well, this is a way i found that this could be possible. The file and directory are not variable, are fixed and the user would have to edit the code in the program each time it wanted another file and/or another directory to work with.</p>
  <p>I recently asked a question about my program and as promissed i would share it when it would be completed.</p>
  <p>The program finds the duplicates in a folder and sub-folders of the directory chosen, lists them by order from ancient to the most recent one and it gives the user the choice to eliminate the files, file by file, at the user's will (telling the user which file is up to being deleted/eliminated).</p>
  <p>Im very new to this kind of programming so if it is not the prettiest of programs im sorry, but it works and i haven't found something like this anywhere. I hope this helps someone!</p>
  <pre><code>    @echo off
      setlocal EnableDelayedExpansion
      
      set &quot;source_folder=C:\Desktop\Batch testes\Teste 1\Giorgio.docx&quot;
      
      echo Welcome to this Practice/Test Program
      echo This program has the obective of finding duplicated files. 
      pause
      echo The user will have the chance to eliminate these files if he wishes
      echo There will be the identification of the oldest and the newest file
      pause
      
      echo Let us start...
      pause
      
                
      :aPrompt
      cls
      echo ========================================
      echo What do you wish to do?
      echo 1.Show only the duplicated files. (Clique '1')
      echo 2.Show the duplicated files and eliminate them.  (Clique '2')
      echo 3.Exit the program (Clique '3')
      
      
      
      choice /n /c:123 /M &quot;Escolha uma opcao &quot;%1
      goto Alfa-%errorlevel%
      
      
      :Alfa-1 A1
      
         
         echo Showing the files being analysed
      
         forfiles /P &quot;C:\Desktop\Batch testes\Teste 1&quot; /M Giorgio.docx /S /C &quot;cmd /c echo @file&quot;
      
         echo Showing the paths of the files being analysed
         
         forfiles /P &quot;C:\Desktop\Batch testes\Teste 1&quot;  /M Giorgio.docx /S /C &quot;cmd /c echo @relpath@fname&quot;
      
         pause
         echo ===============================================================================
         echo.         
         echo The files are set by order from the oldest (on top) to the most recent (on the bottom)         
     echo.
     (for /f &quot;delims=&quot; %%a in ('dir /a-d /t:w /s /b &quot;C:\Desktop\Batch testes\Teste 1\*Giorgio.docx&quot;') do @echo %%~Ta %%a) 
        
     echo.
     echo The oldest file is on top of the list. The most recent file is at the bottom of the list
         
         
      
       
        echo.
        echo=======================
        echo=======================
        pause
           
         :bPrompt
         echo Here are the duplicated files
         echo Do you wish to leave or go back? (Type '1' to end the program, '2' to return to the previous menu)
         
        
         
         choice /n /c:12 /M &quot;Pick an option &quot;%1
         goto Bravo-%errorlevel%
         :Bravo-1 B
            echo The program will now end
            pause
            exit
         
         :Bravo-2 B
            echo You chose to go back
            pause
            goto :aPrompt
      
      :Alfa-2 A2
      
         echo You chose to show and eliminate the duplicated files
         pause
         echo Showing the files paths
         forfiles /P &quot;C:\Desktop\Batch testes\Teste 1&quot;  /M Giorgio.docx /S /C &quot;cmd /c echo @relpath@fname&quot; 
         
         echo Files identyfied by the the program (it serves the purpose of confirming if they are the right files)
         
         forfiles /P &quot;C:\Desktop\Batch testes\Teste 1&quot; /M Giorgio.docx /S /C &quot;cmd /c echo @file&quot;
      
          echo The files are set by order from the oldest (on top) to the most recent (on the bottom)         
     echo.
     (for /f &quot;delims=&quot; %%a in ('dir /a-d /t:w /s /b &quot;C:\Desktop\Batch testes\Teste 1\*Giorgio.docx&quot;') do @echo %%~Ta %%a) 
        
     echo.
     echo The oldest file is on top of the list. The most recent file is at the bottom of the list
         
         echo You sure you want to delete the files? (Type '1' to eliminate them, '2' to not eliminate)
         
         :cPrompt
         choice /n /c:12 /M &quot;Pick an option &quot;%1
         goto Charlie-%errorlevel%
         :Charlie-1 C1
         
             echo You chose to delete the files
             
             
             echo The files will be deleted according to your decision
             echo For each file it will be asked if you wish to delete it
             pause
             del /p /s &quot;C:\Desktop\Batch testes\Teste 1&quot;\*Giorgio.docx 
             echo The files have been deleted at your choice
             pause
             :dPrompt
             echo Do you wish to exit the program or go back? (Type '1' to end the program, type '2' to go back to the main menu)
             
             choice /n /c:12 /M &quot;Pick an option &quot;%1
             goto Delta-%errorlevel%
             :Delta-1 D1
             
             
                 echo You chose to leave. The program will shut down
                 pause
                 exit
             
             :Delta-2 D2
            
             
                 echo You chose to go back to the main menu
                 pause
                 goto :aPrompt
         
         :Charlie-2 C2
         
             :ePrompt
             echo You chose not to delete the files.
             echo Do you wish to go back to the main menu or to leave? (Type '1' to go back to the main menu, '2' to leave the program)
             
             
             choice /n /c:12 /M &quot;Pick an option &quot;%1
             goto Evo-%errorlevel%
             :Evo-1 E1
             
                 echo You chose to go back to the main menu
                 pause
                 goto :aPrompt
             
             :Evo-2 E2
                echo You chose to exit the program. The program will shut down now
                pause
                exit
             
             
      :Alfa-3 A3
      
          echo The program will shut down then
          pause
          exit
  </code></pre>
  <p>If by any case something is failing/giving an error, tell me and i'll fix it because i may have copied something wrong by mistake (like a typo or a missing &quot;(&quot;, something).
  ( I don't know how i could share my program without using the &quot;ask a question&quot; function, im sorry if i broke any rules of this website)</p>
  <p>Special thanks to Stephan that helped me previously and without him i couldn't complete the part where i set the oldest and recent file!</p>