简体   繁体   English

如何在 Windows/命令行/文件打开对话框下匹配确切的文件扩展名?

[英]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.exeabcdefoo.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,您可以使用来自适用于 LinuxWindows 子系统的 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.

相关问题 如何从 Windows 下的命令行打开文件进行编辑? - How do I open a file for edit from the command line under Windows? 在 PowerShell 上运行 npm 询问“你想如何打开这个文件?”,命令行很好 - Running npm on PowerShell asks "How do you want to open this file?", command line is fine Windows 命令行搜索与目录的精确扩展 - Windows command line search for exact extension with dir 如何从Windows命令行shell脚本中打开(锁定)文件? - How do I hold a file open (locked) from a Windows command-line shell script? windows命令行:用活动进程打开文件? - windows command line: open file with active process? 如何在Windows XP中从命令行打开“查找文件”对话框以搜索特定文件夹? - How do I open “Find Files” dialog from command-line in Windows XP to search a specific folder? Docker 桌面 Windows - 您想如何打开此文件? - Docker Desktop Windows - How do you want to open this file? 如何防止双击打开文件对话框注册点击它下面的表单? - How do I prevent a double-click on an Open File dialog from registering a click on the form under it? 使Windows 8显示“如何打开”对话框? - Make windows 8 display the “How do you want to open” dialog? 在Windows中以编程方式或从命令行显示文件属性对话框 - Show file properites dialog programatically or from command line in windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM