Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file: Deleting duplicate files within similar folder structures  (Read 12855 times)

0 Members and 1 Guest are viewing this topic.

JLAman

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Windows 7
    Create a batch file to only delete duplicate files (same filename, date/time) in the same relative folder location.  New folder\files along with the .bat file are placed here: \TEST\NEW\.  Old files are in \TEST\OLD\.
    Folder \OLD\ contains many folders that have the same structure as the folder under \NEW\.  The intent is to delete all of the duplicate files under \OLD\ which are the same filename, date\time, and relative location in the folder structure.
    \TEST\OLD\1\15\1a.txt or \TEST\OLD\3\15\1a.txt would be deleted if \TEST\NEW\1\15\1a.txt existed and has the same date\time. 

    I started with which does not seem to work -

    Code: [Select]
    for %%F in ("d:\test2\new\**") do (
        if exist "d:\test2\old\%%~nxF" del "d:\test2\old\%%~nxF"
    )
    « Last Edit: October 09, 2019, 03:14:31 PM by patio »

    Salmon Trout

    • Guest
    Re: Batch file: Deleting duplicate files within similar folder structures
    « Reply #1 on: October 10, 2019, 10:08:36 AM »
    Your script only deletes files in D:\OLD\ (just that folder) if the same name file exists in D:\NEW\ (just that folder). However, you seem to want to go deeper in the folders, and there is nothing about file date in your script.

    JLAman

      Topic Starter


      Newbie

      • Experience: Beginner
      • OS: Windows 7
      Re: Batch file: Deleting duplicate files within similar folder structures
      « Reply #2 on: October 16, 2019, 10:27:34 AM »
      This is what I've been able to research/apply.  As before, the following deletes all occurrences of 1a.txt within the \Old\ path structure regardless of file modified date or size and path.  Even if a file of the same name/date/size exist within the \Old\ structure, only those which also match the path from the fourth level and below are to be deleted.

      Code: [Select]
      SETLOCAL enableextensions
      pushd "D:\test2\old\"
      for /F "delims=" %%G in ('dir /B /S /A:-D *.*') do (
        call :FileComp "%%~fG" "D:\test2\new\%%~nxG"
      )
      popd
      ENDLOCAL
      goto :eof

      :UpError
      exit /B %1

      :FileComp
        call :UpError 321
        fc /B "%~1" "%~2" >NUL 2>&1
        if not %errorlevel% EQU 0 (
          del "%~1"
        ) else (
          echo %errorlevel% "%~2"
        )
      goto :eof

      Hackoo



        Hopeful
      • Thanked: 42
      • Experience: Expert
      • OS: Windows 10
      Re: Batch file: Deleting duplicate files within similar folder structures
      « Reply #3 on: October 16, 2019, 09:47:27 PM »
      Hi  ;)
      I don't know if this can be helpful to you in your situation ==> Compare files from two folders using HASH SHA1 in BATCH

      SpectateSwamp



        Newbie

        Re: Batch file: Deleting duplicate files within similar folder structures
        « Reply #4 on: November 26, 2019, 11:04:04 AM »
        I'd use long filenames so that every copy would have a unique name
        Put them all in one folder
        and look at them in SIZE order..
        manually deleting those that are duplicates.