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

Author Topic: Batch file to delete duplicate files in 2 folders  (Read 11205 times)

0 Members and 1 Guest are viewing this topic.

mern1

    Topic Starter


    Rookie

    Batch file to delete duplicate files in 2 folders
    « on: July 17, 2008, 07:08:14 AM »
    I need help composing a batch file that will compare the contents of two folders ("A" & "B") and delete the files from "B" that also appear in "A".  Leaving only one file in the "A" folder.  Just to make things a little more complicated the file name structure is as follows:

    filename.prt.1

    Each time the files are saved they are duplicated and the number extension at the end will be incremented (previous files are retained, just not used).  I need for that number extension to be ignored. 

    I hope all this is clear enough.  Any help would be greatly appreciated.  Thanks

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Batch file to delete duplicate files in 2 folders
    « Reply #1 on: July 17, 2008, 04:12:03 PM »
    You lost me with the number extension. If you strip off the number from the files in FolderA, how will you ever match the names in FolderB where the files presumably still have their numbers?

    Otherwise you have three possibilities: Filename is in A, Filename is in B, Filename is in A & B. I try not to post destructive code, so you'll need to replace the echo statements  with del instructions.

    Code: [Select]
    @echo off
    for /f "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /b') do (
    if exist "c:\FolderB\%%v" echo File %%v in FolderA; File %%v in FolderB
    if not exist "c:\FolderB\%%v" echo File %%v in FolderA; File %%v NOT in FolderB
    )

    for /f "tokens=* delims=" %%v in ('dir c:\FolderB\*.* /b') do (
    if exist "c:\FolderA\%%v" echo File %%v in FolderA; File %%v in FolderB
    if not exist "c:\FolderA\%%v" echo File %%v NOT in FolderA; File %%v in FolderB
    )

    As for the number increment, there are worst ways to spend your summer. 8)

    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    mern1

      Topic Starter


      Rookie

      Re: Batch file to delete duplicate files in 2 folders
      « Reply #2 on: July 28, 2008, 02:30:40 PM »
      The code listed above works but I still need more help.  The file name structure is as follows:

      filename.ext1.#

      Example:
      3348053.prt.1
      3348053.prt.2
      ect...

      I need to be able to strip off the last extension (the number) and search the folders using only the file name and the first extension. 

      Each time that the program (Pro/Engineer) saves a file it actually creates a copy.  The last extension of the file name of the new file is then incremented.  Giving you a complete history of your work (and a giant pain for file management).

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: Batch file to delete duplicate files in 2 folders
      « Reply #3 on: July 28, 2008, 03:31:20 PM »
      Just did one side of it. You can do the other.

      Code: [Select]
      @echo off
      for /f "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /b') do (
      if exist "c:\FolderB\%%~nv" echo File %%~nv in FolderA; File %%~nv in FolderB
      if not exist "c:\FolderB\%%~nv" echo File %%~nv in FolderA; File %%~nv NOT in FolderB
      )

      These code notations for paths/files may help you out.

      Quote
      %~I         - expands %I removing any surrounding quotes (")
      %~fI        - expands %I to a fully qualified path name
      %~dI        - expands %I to a drive letter only
      %~pI        - expands %I to a path only
      %~nI        - expands %I to a file name only
      %~xI        - expands %I to a file extension only
      %~sI        - expanded path contains short names only
      %~aI        - expands %I to file attributes of file
      %~tI        - expands %I to date/time of file
      %~zI        - expands %I to size of file
      %~$PATH:I   - searches the directories listed in the PATH
                     environment variable and expands %I to the
                     fully qualified name of the first one found.
                     If the environment variable name is not
                     defined or the file is not found by the
                     search, then this modifier expands to the
                     empty string

       8)

      PS. With multi-dot file names, the extension is the data after the last dot.
         
      « Last Edit: July 28, 2008, 04:12:56 PM by Sidewinder »
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      mern1

        Topic Starter


        Rookie

        Re: Batch file to delete duplicate files in 2 folders
        « Reply #4 on: July 29, 2008, 06:51:55 AM »
        I'm not going to pretend to really know whats going on with this code, but I think something is still missing.  After changing the %%v to %%~nv the code strips off the extension of the file names in folderA and searches folderB for the file without the extension.  The only problem is all the files in folderB still have their extensions.  So the batch file says that none of the files in folderA are in folderB (even though that's not the case).  The batch file needs to strip off the file name extensions in both folders and then compare them.  I appologize for not making this clear before.  Is this possible?  Thanks!

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: Batch file to delete duplicate files in 2 folders
        « Reply #5 on: July 29, 2008, 07:35:18 AM »
        Could you please post a sample of the file names in both FolderA and FolderB. Unless I'm missing something basic, none of this makes any sense. Stripping off the increment number in the file name, guarantees duplicate file names.

        Another approach would be to strip the extension in FolderA. By using the extensionless filename with a wildcard, you could grab all the files from FolderB with the same base name.

         8)
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        mern1

          Topic Starter


          Rookie

          Re: Batch file to delete duplicate files in 2 folders
          « Reply #6 on: July 29, 2008, 11:08:27 AM »
          Your wildcard suggestion works perfectly.  Thanks for all your help, I really appreciate it!