Deleting items via the command-line is pretty easy.

del /options filename.extension

Now, suppose I want to delete all files which do not end with .jpg in a folder, how would I do that.

The thing is, I have a piece of software that converts all specified images to .jpg, but it leaves the originals, which I don't need anymore.

It would be much more efficient to execute a single statement, compared to doing multiple statements for every different filetype.


I would do it like this:

attrib +r *.jpg
del /q *
attrib -r *.jpg

This will first make all JPG files read-only, delete everything else (it will automatically skip read-only files), and then make the JPG files writeable again.

  • Worked like a champ. Note: just add /s to each line to recursively include files in subdirectories. – Chuck Wilbur Oct 16 '17 at 18:57

That's actually pretty easy.

You'll need for to iterate over the files and then simply look for the extension:

for %f in (*) do if not %~xf==.jpg del "%f"

should do the trick (code here).

  • thanks, but that alone depends on which map I'm using. – KdgDev Aug 30 '09 at 21:24
  • 4
    Map? – Joey Aug 31 '09 at 5:55
  • How do I say if not .jpg and not .jsp and not .xml then del ? – user1164061 Mar 3 '17 at 20:45

Powershell to the rescue
In times of Windows 7/8 it's the successor of the good old command line

Del C:\myFolder\* -exclude '*.jpg'

Del is an alias for Remove-Item. It has several options like recurse, include, exclude and filter (use this for RegEx)

You have to add \* to include files in a given folder

  • From the CMD prompt, this should work: PowerShell.exe -Command "&{Remove-Item C:\MyFolder\* -Exclude '*.jpg'}" – Iszi Sep 23 '14 at 17:16
  • Note: PowerShell comes with Win 7/8. For Windows XP or Vista, you need to download and install it. – Iszi Sep 23 '14 at 17:18

I know it's not answering your question directly, but have you looked at the options on your converter to see if:

  1. It can delete the originals itself


  1. Write the .jpg's to a new folder?
  • I doesn't have the direct option to do so. What it will do is overwrite the original if that was a JPG to begin with. But all others remain on the disk while a JPG of them is created. – KdgDev Aug 30 '09 at 21:22
  • @WebDevHobo - oh well, I'd go with Johannes suggestion then. – ChrisF Aug 30 '09 at 21:24

I was looking for a way to find all files that did NOT have the extension ".mp3" in a directory TREE on Windows 7 (NTFS volume) containing perhaps 20,000 files in several hundred directories of various depths... so after a bit of angst, I used:

cd <theplace>
dir /S | find /V "<DIR>" | find /V "Total" | find /V "bytes" | find /V "Directory" | find /V "Volume" | find /V ".mp3" | more /S

this listed the files that did not match the .mp3 after stripping out everything related to the DIR command output... 99% works... unless the file that doesn't match is named one of the keywords in the standard DIR output - perhaps there's a way to make DIR report less header/summary info - but I didn't bother as this got most of the way there.

  • apparently the Posting code here strips some special characters... so the "cd" command above was on a line by itself and specified the target root directory, and the first "find" command removed any lines that contained "<" DIR ">" (go figure) since that's the output of the dir command - but the post code here may think that's some sort of meta-instruction and strips it... – CaptPat Feb 18 '12 at 14:38
  • 1
    There is a much easier way to do this: the /b flag of the dir command. dir /b will list filenames and extensions in the current directory only, dir /s /b will list the fully qualified names of all files in the current and all subdirectories. Piping to find /v instead of a for loop is a nice touch I'll have to remember, but I don't think it works with del. – Bob Feb 18 '12 at 15:09

Another way to delete all files matching a filename is shown below. The for loop list's all files and the if NOT matches by filename (without directory name).

for /r %f in (*) do if not %~nxf == abc.xml del "%f"

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.