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.

link|improve this question

feedback

4 Answers

up vote 5 down vote accepted

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|improve this answer
+1 because unlike Joey's works for any pattern! – ybungalobill May 1 '11 at 8:49
feedback

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|improve this answer
thanks, but that alone depends on which map I'm using. – WebDevHobo Aug 30 '09 at 21:24
Map? – Joey Aug 31 '09 at 5:55
feedback

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

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.

link|improve this answer
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 at 14:38
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 at 15:09
feedback

Your Answer

 
or
required, but never shown

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