1.

Solve : Batch file to delete duplicate files in 2 folders?

Answer»

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. ThanksYou 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.

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).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



PS. With multi-dot file names, the extension is the data after the last dot.

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!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.

Your wildcard suggestion works perfectly. Thanks for all your help, I really appreciate it!


Discussion

No Comment Found