How to differentiate between similar filename extensions?

Microsoft Windows
Post Reply
Rekrul
Posts: 52
Joined: 2021-Aug-15, 11:29 pm

How to differentiate between similar filename extensions?

Post by Rekrul »

I have just discovered, much to my surprise, that when you use a wildcard for the filename and you enter three characters for the extension, it will match every extension that starts with those characters.

For example, telling it to look for;

*.jpe

Will also match every file that ends in;

.jpeg

Yet, if I enter;

*.jp

It doesn't match anything (unless there happens to be a filename that ends in exactly .jp).

Is there any way to differentiate between the extensions, so that it will only match what I've entered and not treat three-character extensions as a wildcard?
bluesxman
Posts: 10
Joined: 2021-Jul-26, 3:41 pm

Re: How to differentiate between similar filename extensions?

Post by bluesxman »

I don't think it's treating it as a wildcard exactly, I think you're probably coming up against weirdness from 8.3 filenames

Code: Select all

S:\>dir /x
<snip>
12/04/2022  16:32                 2              hello.jpe
12/04/2022  16:32                 2 HELLO~1.JPE  hello.jpeg
12/04/2022  16:32                 2              hello.txt
12/04/2022  16:32                 2 HELLO~1.TXT  hello.txt1
12/04/2022  16:35                 2 HELLOW~1.TXT hellowoworld.txt
12/04/2022  16:35                 2 HELLOW~2.TXT hellowoworld.txt1
<snip>
Without any context of what you're trying to do, it's difficult to offer a solution. However, FOR seems to be able to see through the fog (or rather it seems to be entirely oblivious to 8.3)

Code: Select all

S:\>for %F in (*) do @(if "%~xF" EQU ".txt" echo %F)
hello.txt
hellowoworld.txt
S:\>
qwerty
Posts: 7
Joined: 2021-Dec-20, 2:40 pm

Re: How to differentiate between similar filename extensions?

Post by qwerty »

Another way to do this is with the where command. To search from your present directory:

Code: Select all

where ".:*.jpe"
You can replace the period with the exact file location if you want to search another directory:

Code: Select all

where "C:\Users:*.jpe"
Another option is forfiles:

Code: Select all

forfiles /M *.jpe
User avatar
Simon Sheppard
Posts: 190
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: How to differentiate between similar filename extensions?

Post by Simon Sheppard »

Confirmed, if you turn off 8.3 generation using:

Code: Select all

FSUTIL.exe behavior set disable8dot3 1
Then DIR *.jpe will not return the jpeg files.

Also 8.3 filename generation is turned off by default in all new (fresh install) Windows installations.
Rekrul
Posts: 52
Joined: 2021-Aug-15, 11:29 pm

Re: How to differentiate between similar filename extensions?

Post by Rekrul »

bluesxman wrote: 2022-Apr-12, 3:36 pmWithout any context of what you're trying to do, it's difficult to offer a solution.
I was trying to count each type of file in a given directory by using for loops to run through each extension, but the .jpeg files were being double-counted.

I already have a workaround, I was just wondering if there was a simple way to stop it from happening in case there's a future case where I want two similar extensions to be processed differently.
bluesxman wrote: 2022-Apr-12, 3:36 pmHowever, FOR seems to be able to see through the fog (or rather it seems to be entirely oblivious to 8.3)
Yes, %%~xF will return the true extension, but then I have to include a bunch of IF statements to test for various types, which slows it down if processing a lot of files.
qwerty wrote: 2022-Apr-12, 5:07 pm Another way to do this is with the where command. To search from your present directory:

Another option is forfiles:
I'm doing this on an old system under WinXP and I don't have either of those commands.

Apparently they're included in the "Windows 2000 Resource Kit", which I've never been able to find an official download page for, since Microsoft seems to have scrubbed all downloads for older versions of Windows from their web sites.
Simon Sheppard wrote: 2022-Apr-12, 5:12 pm Confirmed, if you turn off 8.3 generation using:

Code: Select all

FSUTIL.exe behavior set disable8dot3 1
Then DIR *.jpe will not return the jpeg files.

Also 8.3 filename generation is turned off by default in all new (fresh install) Windows installations.
I am extremely leery of messing with the way Windows handles filenames.

Funny story: On my old Windows 98 system, one day Scandisk reported a long filename error, but didn't tell me what file it was. I didn't know how to find out, so every time Scandisk ran, it would stop with that same error. When I googled the problem, I found a recommendation to edit the Scandisk perferences and turn off "Validate long filenames".

HUGE MISTAKE!!! ***NEVER*** DO THIS ON A WINDOWS 98 SYSTEM!!!

The next time Scandisk ran, it was taking a very long time and after I stopped it and the system booted, I discovered that it had been in the process of going through my entire C: drive and renaming EVERY file to its 8.3 name! Needless to say, this royally screwed up my system.

I also discovered that while a record of the long filenames still existed on the drive, there are absolutely no tools that will restore them in bulk. I found one program that would show them to you and another that would recover one file at a time with its long filename, but nothing else. I even contacted the company who made the latter tool and asked if the full, commercial version of the program would let me do it and was informed that no, that ability was not in the full version!

All of this makes me extremely hesitant to alter the way that Windows handles filenames on an existing system.
bluesxman
Posts: 10
Joined: 2021-Jul-26, 3:41 pm

Re: How to differentiate between similar filename extensions?

Post by bluesxman »

Maybe something like this?

Code: Select all

for %%F in (*) do set /a "EXT%%~xF+=1"
set EXT.
This should count all the extensions, if you only care about a few you can just pick those up like %EXT.jpe%
User avatar
Simon Sheppard
Posts: 190
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: How to differentiate between similar filename extensions?

Post by Simon Sheppard »

Rekrul wrote: 2022-Apr-14, 12:21 am All of this makes me extremely hesitant to alter the way that Windows handles filenames on an existing system.
Turning off 8.3 filename generation does not affect any pre-existing files, it simply means that all new files you generate will have long filenames only.
So you can turn it off, copy the files into a fresh folder so the new copies lose the 8.3 names, run your script and then turn 8.3 generation back on again.
Post Reply