vote up 1 vote down star

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.

flag

3 Answers

vote up 2 vote down check

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.

link|flag
vote up 4 vote down

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

link|flag
thanks, but that alone depends on which map I'm using. – WebDevHobo Aug 30 at 21:24
Map? – Johannes Rössel Aug 31 at 5:55
vote up 1 vote down

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

a) It can delete the originals itself

or

b) Write the .jpg's to a new folder?

link|flag
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. – WebDevHobo Aug 30 at 21:22
@WebDevHobo - oh well, I'd go with Johannes suggestion then. – ChrisF Aug 30 at 21:24

Your Answer

Get an OpenID
or
never shown

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