How do you match an exact file extension under Windows/command line/file open dialog?

孤人 提交于 2021-01-01 04:52:33

问题


I am trying to get a directory listing of only files with a given extension. At first blush this seems to be a simple thing to do, however check out this simple example:

C:\CODE\metcal>dir /b *.exe
metcal.exe
metcal.exe1

Notice that this returns metcal.**exe** and metcal.**exe1** as matches.

With python files a similar thing happens:

C:\CODE\metcal>dir /b *.py
metcal.py
metcal.pyc

Notice again Windows has determined that *.py takes anything that starts with *.py so it captures the .pyc files as well.

Is there a way to get only the extensions that match exactly? In the above python files example I would like the following to occur (obviously with the correct syntax substituted for *.py)

C:\CODE\metcal>dir /b *.py
metcal.py

As a note the matching under Windows not as simple as it seems.

*.exe matches foo.exe, foo.exe1, foo.exeabcde but not foo.exe.bak

There are other questions on SO that are similar that are related to long/short file names. The *.py and *.pyc example here should not introduce name mangling machinery.

**I have experimented on XP and Win7 machines and this behavior is not consistent at the cmd Prompt and file open dialogs. This inconsistant behavior makes me suspect this problem is related to settings of somekind. **


回答1:


It's because windows wildcards on extensions check both long and short names as explained in this answer: https://superuser.com/questions/238900/winxp-dir-command-3-and-4-char-extensions-are-the-same#238930

Solution there is to disable 8.3 names creation and then striping them on ntfs volumes which will also improve performance.

Microsoft Docs: Fsutil 8dot3name

Remarks: Permanently removing 8dot3 file names and not modifying registry keys that point to the 8dot3 file names may lead to unexpected application failures, including the inability to uninstall an application. It is recommended you first back up your directory or volume before you attempt to remove 8dot3 file names.




回答2:


So if you want to get only those extensions (.py and .pyc), you should try like this way :

@echo off
dir /b *.py*
pause


来源:https://stackoverflow.com/questions/38505368/how-do-you-match-an-exact-file-extension-under-windows-command-line-file-open-di

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!