Friday, 9 August 2019

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


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.



Answer



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.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...