I'm trying to write a Windows Batch Script file that deletes just .asp files.

If I do:

del *.asp /s

This has the side-effect of deleting any files with the extension .aspx. This is bad because I want to preserve files with the aspx extension.

Is there any solution to this problem that doesn't require installing a custom application?

3 Answers 3


This seems to do the trick on Vista:

for %i in (*.asp) do @if not %~xi==.aspx del %i

For more information

help for

There are all kinds of modifiers to variables:

In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string

I suspect that because del is a DOS-vintage command, it doesn't understand extensions longer than 3 characters.

There are several solutions I can see, only one of which you'll like and none of which fits your exact requirements:

  • run manually and add the /p switch to prompt and skip aspx files
  • install powershell (presuming you're on XP or newer), which should have a way to iterate over the files in a more fine-grained manner. I'm not a powershell guru, but this may be the best choice because it's at least a Windows component instead of a custom application
  • install GNU findutils and coreutils and delete the files the way you would on unix: "find directoryname -name '*.asp' | xargs rm". Upside: it will work. Downside: it is most definately custom
  • write something in Perl or C or Java if you already have those installed on the box to do the same thing as you'd do in powershell
  • There is no del.exe (nor, as far as I can recall, has there ever been) it is build into cmd.exe (or command.com).
    – Richard
    Jul 20, 2009 at 9:49
  • Fair enough; the point still stands. Aside: try running del.exe - just make sure you're not in a directory that magically contains a file named ".exe". Ouch.
    – James F
    Jul 20, 2009 at 11:04

Powershell will do this. While dir -r *.asp will match *.aspx, you can easily refine the output:

dir . -r -inc *.asp

Using -include to match wildcards in PSH rather than -filter (normally faster) which matches with the Win32 API (with the same results as cmd.exe).

You presumably only want to delete files, so, for testing:

dir . -r -inc *.asp | ?{-not $_.PSIsContainer} | del -whatif

Use of -whatif to just list what would happen. Use -confirm to prompt to configurm the file deletes. For use in a scrip avoiding aliases, but listing what is done:

Get-ChildItem -path . -recurse -include *.asp | 
  Where-Object {-not $_.PSIsContainer} |
  Remove-Item -Verbose

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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