[英]How do you match an exact file extension under Windows/command line/file open dialog?
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.请注意,这将返回
metcal.**exe**
和metcal.**exe1**
作为匹配项。
With python files a similar thing happens:使用 python 文件会发生类似的事情:
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.再次注意,Windows 已确定
*.py
接受以*.py
开头的任何内容,因此它也捕获.pyc
文件。
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)
在上面的python文件示例中,我希望发生以下情况(显然用正确的语法代替*.py)
C:\CODE\metcal>dir /b *.py
metcal.py
As a note the matching under Windows not as simple as it seems.请注意,Windows 下的匹配并不像看起来那么简单。
*.exe
matches foo.exe, foo.exe1, foo.exeabcde
but not foo.exe.bak
*.exe
匹配foo.exe, foo.exe1, foo.exeabcde
但不foo.exe.bak
There are other questions on SO that are similar that are related to long/short file names. SO上还有其他与长/短文件名相关的类似问题。 The *.py and *.pyc example here should not introduce name mangling machinery.
此处的 *.py 和 *.pyc 示例不应引入名称修改机制。
**I have experimented on XP and Win7 machines and this behavior is not consistent at the cmd Prompt and file open dialogs. **我在 XP 和 Win7 机器上进行了试验,这种行为在 cmd Prompt 和文件打开对话框中不一致。 This inconsistant behavior makes me suspect this problem is related to settings of somekind.
这种不一致的行为让我怀疑这个问题与某种设置有关。 **
**
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这是因为扩展上的 Windows 通配符检查长名称和短名称,如本答案所述: https : //superuser.com/questions/238900/winxp-dir-command-3-and-4-char-extensions-are-the-相同#238930
Solution there is to disable 8.3 names creation and then striping them on ntfs volumes which will also improve performance.解决方案是禁用 8.3 名称创建,然后在 ntfs 卷上对它们进行条带化,这也将提高性能。
Microsoft Docs: Fsutil 8dot3name 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.
备注:永久删除 8dot3 文件名而不修改指向 8dot3 文件名的注册表项可能会导致应用程序意外失败,包括无法卸载应用程序。 It is recommended you first back up your directory or volume before you attempt to remove 8dot3 file names.
建议您在尝试删除 8dot3 文件名之前先备份您的目录或卷。
So if you want to get only those extensions ( .py
and .pyc
), you should try like this way :因此,如果您只想获得那些扩展名(
.py
和.pyc
),您应该像这样尝试:
@echo off
dir /b *.py*
pause
You can use the Unix ls
command from the Windows Subsystem for Linux to do this, assuming you have WSL installed.假设您已安装 WSL,您可以使用来自适用于 Linux的Windows 子系统的 Unix
ls
命令来执行此操作。 It's freely available from Microsoft.它可以从 Microsoft 免费获得。 From your Windows command prompt, type
wsl
followed by the ls
command to list the files you want.在 Windows 命令提示符下,键入
wsl
后跟ls
命令以列出所需的文件。
So, for your example, wsl ls metcal.py
returns only metcal.py.因此,对于您的示例,
wsl ls metcal.py
仅返回metcal.py。 To get the same results as you're seeing with dir
, use wsl ls metcal.py*
.要获得与使用
dir
看到的结果相同的结果,请使用wsl ls metcal.py*
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.