2

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?

Simon Johnson
  • 405
  • 1
  • 4
  • 9

3 Answers3

4

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
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
1

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
James F
  • 6,689
  • 1
  • 26
  • 24
  • 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 '09 at 09: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 '09 at 11:04
0

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
Richard
  • 5,324
  • 1
  • 23
  • 20