简体   繁体   中英

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:

del /S  "*.res"

But it deleted all *.RESX files in addition too (ooh, my luckless C# projects ...).

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.

Question : is there simple way to force the batch file to consider the complete filenames, not 8.3 filenames?

At least to 'say' that the length of file extension should be 3 characters exactly.

It's because the dir in cmd.exe uses the FindFirstFile API due to legacy issues. That API matches both long and 8.3 names , hence *.res and *.resx would be the same

The simplest solution is to use powershell

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

It can be called from cmd like (here I replaced the cmdlets with their common aliases)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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