批处理文件问题(具有长文件扩展名的文件名)

Batch file issue (filenames with long file extension)

最近我写了一个简单的BAT文件来删除当前目录及其子目录中所有不需要的*.RES文件:

del /S  "*.res"

但它也删除了所有 *.RESX 文件(哦,我倒霉的 C# 项目...)。

我知道它之前将所有文件名都转换为 DOS 8.3 格式,所以像“Resources.resx”这样的任何文件都将具有 DOS 8.3 文件名“ resour~1.res”,所以不幸的是它会匹配模式“*.res”。

问题:是否有简单的方法强制批处理文件考虑 完整 文件名,不是 8.3 文件名?

至少 'say' 文件扩展名的长度应该正好是 3 个字符。

因为cmd.exe中的dir使用了FindFirstFile API due to legacy issues. That API matches both long and 8.3 names, hence *.res and *.resx would be the same

最简单的解决方案是使用 powershell

Get-ChildItem *.res -Recurse | Remove-Item

可以像这样从cmd调用(这里我用它们的通用别名替换了cmdlets):

powershell -C "ls *.res -r | rm"

纯批处理方案会比较麻烦

for /f "usebackq delims=|" %%f in (`dir /s /b ^| findstr /i /v /r "\.res.$"`) do del %%f