14

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.

6 Answers 6

20

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.

1
  • Worked like a champ. Note: just add /s to each line to recursively include files in subdirectories. Commented Oct 16, 2017 at 18:57
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).

3
  • thanks, but that alone depends on which map I'm using.
    – KdgDev
    Commented Aug 30, 2009 at 21:24
  • 4
    Map?
    – Joey
    Commented Aug 31, 2009 at 5:55
  • How do I say if not .jpg and not .jsp and not .xml then del ? Commented Mar 3, 2017 at 20:45
2

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

2
  • 1
    From the CMD prompt, this should work: PowerShell.exe -Command "&{Remove-Item C:\MyFolder\* -Exclude '*.jpg'}"
    – Iszi
    Commented Sep 23, 2014 at 17:16
  • Note: PowerShell comes with Win 7/8. For Windows XP or Vista, you need to download and install it.
    – Iszi
    Commented Sep 23, 2014 at 17:18
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?
2
  • 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
    Commented Aug 30, 2009 at 21:22
  • @WebDevHobo - oh well, I'd go with Johannes suggestion then.
    – ChrisF
    Commented Aug 30, 2009 at 21:24
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.

2
  • 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
    Commented Feb 18, 2012 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
    Commented Feb 18, 2012 at 15:09
0

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"

You must log in to answer this question.

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