我试图写一个Windows批处理脚本文件, 只删除.asp文件。
如果我做:
del *.asp /s
这有删除任何扩展名为.aspx的文件的副作用。 这是不好的,因为我想用aspx扩展名来保存文件。
有没有解决这个问题,不需要安装自定义应用程序?
这似乎是在Vista上做的伎俩:
for %i in (*.asp) do @if not %~xi==.aspx del %i
了解更多信息
help for
variables有各种修饰符:
另外,FORvariables引用的replace已被增强。 您现在可以使用以下可选语法:
%~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
我怀疑是因为del是DOS复制命令,它不能理解长度超过3个字符的扩展名。
我可以看到几种解决scheme,其中只有一种您会喜欢,其中没有一种符合您的具体要求:
PowerShell会做到这一点。 虽然dir -r *.asp将匹配*.aspx ,您可以轻松地完善输出:
dir . -r -inc *.asp
使用-include匹配PSH中的通配符,而不是匹配Win32 API(与cmd.exe具有相同结果)的-filter (通常更快)。
你大概只想删除文件,所以为了testing:
dir . -r -inc *.asp | ?{-not $_.PSIsContainer} | del -whatif
使用-whatif来列出将要发生的事情。 使用-confirm提示configuration文件删除。 用于避免别名的脚本,但列出了所做的事情:
Get-ChildItem -path . -recurse -include *.asp | Where-Object {-not $_.PSIsContainer} | Remove-Item -Verbose