Deleting all files that do not match a certain pattern - Windows command line

Solution 1:

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.

Solution 2:

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

Solution 3:

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