简体   繁体   English

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

[英]Batch file issue (filenames with long file extension)

Recently I have written a simple BAT file to delete all unnecessary *.RES files from the current directory and its sub-directories:最近写了一个简单的BAT文件,删除当前目录及其子目录下所有不需要的*.RES文件:

del /S  "*.res"

But it deleted all *.RESX files in addition too (ooh, my luckless C# projects ...).但它也删除了所有 *.RESX 文件(哦,我倒霉的 C# 项目......)。

I understand that it converted all filenames to DOS 8.3 format before, so any file like " Resources.resx " will have DOS 8.3 filename " resour~1.res ", so it will match the pattern "*.res" unfortunately.我知道它之前将所有文件名转换为 DOS 8.3 格式,因此任何像“ Resources.resx ”这样的文件都将具有 DOS 8.3 文件名“ resour~1.res ”,因此不幸的是它会匹配模式“*.res”。

Question : is there simple way to force the batch file to consider the complete filenames, not 8.3 filenames?问题:有没有简单的方法可以强制批处理文件考虑完整的文件名,而不是 8.3文件名?

At least to 'say' that the length of file extension should be 3 characters exactly.至少要“说”文件扩展名的长度应该是 3 个字符。

It's because the dir in cmd.exe uses the FindFirstFile API due to legacy issues.这是因为 cmd.exe 中的dir由于遗留问题使用FindFirstFile API。 That API matches both long and 8.3 names , hence *.res and *.resx would be the same该 API匹配长名称和 8.3 名称,因此*.res 和 *.resx 将相同

The simplest solution is to use powershell最简单的解决方案是使用powershell

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

It can be called from cmd like (here I replaced the cmdlets with their common aliases)它可以像 cmd 一样调用(这里我用它们的通用别名替换了 cmdlet)

powershell -command "ls *.res -r | del"

The pure batch solution would be more cumbersome纯批处理会比较麻烦

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM