What I want to do is have a batch file search folders, and subfolders, for matching file names that are ending in .mp3 and .wma and delete or move the *.wma duplicates.

Below you'll find what I have come up with until now, I know it is wrong for at least two reasons. I have searched the internet, and tried a number of things, but now I am stuck. Can anyone please help me with this?

for /r %%f in "(%userprofile%\my music\* - ?.*)" do del "%%f" /s/p
  • Why not use a free tool from ashisoft duplicate file finder. ashisoft.com
    – user109612
    Dec 14 '11 at 22:12
  • 1
    Because it costs $30.00. the free version will not work on the task I want unless I buy it. I would rather learn how to do it, than throw money at it. But thank you for the tip.
    – Bringo
    Dec 16 '11 at 21:28

I'm no windows guru but forfiles is pretty straight forward.
forfiles /p "%userprofile%\my music\" /s /m *.mp3 /C "cmd /c del @relpath@fname.wma"

  • I appreciate your response Micke. I am not a guru either. I am actually pretty new at writing batch files. I have written a few easy batch files using the basics, but that is all. I have tried your solution, and I think it is much closer to what I need than what I had. However it did not work. I receive an error as follows, ERROR: Invalid argument/option - '@relpath@fname.wma' I would appreciate any other ideas you may have. Thank you.
    – Bringo
    Dec 11 '11 at 0:17
  • Okay, I got it to kind of work. I input it as: FORFILES /P "%USERPROFILE%\MY MUSIC" /S /M *.MP3 /C "CMD /C DEL @FNAME.WMA" It did not like the "\" behind "my music" or "@relpath" with "@fname" for some reason. It does delete *.wma files that have a duplicate *.mp3 file. However, only the same folder, and not in lateral folders. Any ideas? thank you again for your help.
    – Bringo
    Dec 11 '11 at 18:21
  • Try adding @PATH\ before @FNAME instead of @RELPATH. You want it to expand to the path and the filename plus your extension (.WMA) so DEL can run. I'm not at a Windows computer right now so unfortunately I cannot try it myself.
    – micke
    Dec 13 '11 at 22:11
  • That didn't work, or I typed it incorrectly. It works using @fname by itself, but it only deletes *.WMA files in the current directory where an *.MP3 was found, and not in the rest of the directories.
    – Bringo
    Dec 17 '11 at 13:19

May I recommend powershell?

cd C:\users\Administrator\Desktop\test\

$Afiles = ls *.text
$Bfiles = ls *.txt

$Alist = @()
$Blist = @()

foreach( $A in $Afiles)
    $Alist += $A.BaseName

foreach( $B in $Bfiles)
    $Blist += $B.BaseName

foreach($A in $Alist)
    if($Blist -contains $a) 
        rm ("$A.text") -WhatIf

while this may seem a bit more complicated, its much more extensible and reusable IMHO.


Sorry for brining up that old question, but for anyone who stumbles here in the future, check this out, this works in a batch file:




this worked really great for what i needed

forfiles /P C:\Projects\QualityPush\ /S /M  first.txt /C "cmd /c echo @path"
forfiles /P C:\Projects\QualityPush\ /S /M  second.txt /C "cmd /c echo @path"

This will give you the path to each file for any matches in sub dirs and keeps them collated together

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.