Batch file issue (filenames with long file extension)

93 Views Asked by At

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.

1

There are 1 best solutions below

2
On BEST ANSWER

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 -Recurse | Remove-Item

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

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

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