The Computer Oracle

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

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: The World Wide Mind

--

Chapters
00:00 Deleting All Files That Do Not Match A Certain Pattern - Windows Command Line
00:36 Answer 1 Score 1
00:53 Answer 2 Score 9
01:09 Accepted Answer Score 21
01:27 Answer 4 Score 0
02:02 Thank you

--

Full question
https://superuser.com/questions/32500/de...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#windows #commandline

#avk47



ACCEPTED ANSWER

Score 21


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.




ANSWER 2

Score 9


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




ANSWER 3

Score 1


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

or

  1. Write the .jpg's to a new folder?



ANSWER 4

Score 0


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.