Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 10 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.

share|improve this answer
    
+1 because unlike Joey's works for any pattern! – ybungalobill May 1 '11 at 8:49

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

share|improve this answer
    
thanks, but that alone depends on which map I'm using. – KdgDev Aug 30 '09 at 21:24
4  
Map? – Joey Aug 31 '09 at 5:55

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

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.

share|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 '12 at 14:38
1  
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 '12 at 15:09

Another way to delete all files matching a filename is shown below. The for loop list's all files and the if NOT matches by filename (without directory name).

for /r %f in (*) do if not %~nxf == abc.xml del "%f"
share|improve this answer

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

share|improve this answer
    
From the CMD prompt, this should work: PowerShell.exe -Command "&{Remove-Item C:\MyFolder\* -Exclude '*.jpg'}" – Iszi Sep 23 '14 at 17:16
    
Note: PowerShell comes with Win 7/8. For Windows XP or Vista, you need to download and install it. – Iszi Sep 23 '14 at 17:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .