I've found this link How can I delete files that don't match a wildcard? that is pretty similar to what I want to do but all my files have the same extension and I want to do delete all those that don't match a file name with a wildcard. E.g. I want to keep all files that are "DOC*.PDF" but delete all others like Smith.PDF. I've tried modifying the answer given in the link above but haven't had any success.
Asked
Active
Viewed 1,294 times
2 Answers
2
Use PowerShell.
get-childitem *.pdf -exclude doc*.pdf | remove-item -whatif
Naturally, the -whatif
parameter is for testing purposes. Remove it to actually delete the files.

Bill_Stewart
- 22,916
- 4
- 51
- 62
-
The would preferable but I'm on Windows Server 2008 (not R2) without powershell. I should have specified. I need the dos cmd line. – Pistol Pete Feb 16 '15 at 23:48
1
From the command line:
for /f "eol=: delims=" %F in ('dir /b /a-d *.pdf ^| findstr /biv doc') do @del "%F"
The DIR command lists all .pdf files. The result is piped to FINDSTR which discards files that begin with DOC (case insensitive). The result is processed by FOR /F, and each file is deleted individually.
Double the percents if used within a batch script.

dbenham
- 127,446
- 28
- 251
- 390