奇怪的Windows DIR命令行为 [英] Strange Windows DIR command behavior

查看:193
本文介绍了奇怪的Windows DIR命令行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找,在名称中的数字文件发现这纯属偶然。当我键入​​:

I discovered this quite by accident while looking for a file with a number in the name. When I type:

DIR * 数量 *

(其中重$ P $号psents任何数字从0到9,并用星号和数字之间没有空格)

(where number represents any number from 0 to 9 and with no spaces between the asterisks and the number)

在CMD.EXE命令提示符下,它会返回各种文件不会出现在任何适合的搜索条件。有什么奇怪的是,根据该目录,一些数字会工作,而不是别人。一个例子是,在与网站相关的目录,我键入以下内容:

at the cmd.exe command prompt, it returns various files do not appear in any to fit the search criteria. What's weird, is that depending on the directory, some numbers will work and not others. An example is, in a directory associated with a website, I type the following:

dir *4*

和什么返回是:


Directory of C:\Ampps\www\includes\pages 

04/30/2012  03:55 PM               153 inventory_list_retrieve.php
06/18/2012  11:17 AM             6,756 ix.html
06/19/2012  01:47 PM           257,501 jquery.1.7.1.js
               3 File(s)        264,410 bytes
               0 Dir(s)  362,280,906,752 bytes free

这只是没有任何意义了我。任何线索?

That just doesn't make any sense to me. Any clue?

问题是在计算器上提出,因为DIR命令与FOR在批处理程序常常合并。奇怪的DIR行为似乎让批处理程序可能不可靠的,如果他们使用DIR命令。

The question is posed on stackOverflow because the DIR command is often combined with FOR in batch programs. The strange DIR behavior would seem to make batch programs potentially unreliable if they use the DIR command.

编辑:(补充说明)。虽然太多的时间已经过去了,我发现这个几乎花费了我很多工作的另一个怪癖。我想删除特定的目录树中的所有的.htm 文件。我意识到只是做之前 *。HTM 匹配的.html 的文件。此外, *人匹配名为.manifest ,并且有可能是别人。删除所有的.html 文件在特定的目录会一直不安,至少可以说。

(additional note). Though much time has passed, I discovered another quirk with this that almost cost me a lot of work. I wanted to delete all .htm files in a particular directory tree. I realized just before doing it that *.htm matches .html files as well. Also, *.man matches .manifest, and there are probably others. Deleting all .html files in that particular directory would have been upsetting to say the least.

推荐答案

在命令提示符通配符对阵双方长文件名和短匹配8.3,如果名字一个是present。这可能会产生意外的惊喜。

Wild cards at the command prompt are matched against both the long file name and the short "8.3" name if one is present. This can produce surprises.

要看到短名称,使用 / X 选项到 DIR 命令。

To see the short names, use the /X option to the DIR command.

请注意,这种行为是不以任何方式具体到 DIR 命令,当一个通配符匹配多个预期可能导致其他(通常不愉快)惊喜任何命令,如 DEL

Note that this behavior is not in any way specific to the DIR command, and can lead to other (often unpleasant) surprises when a wild card matches more than expected on any command, such as DEL.

不像在* nix的外壳,更换名称匹配的列表的文件模式是每个命令中实现,而不是通过shell本身来实现。这可能意味着不同的命令可以实现不同的外卡模式规则,​​但在实践中,这是相当罕见的,因为Windows提供的API调用来搜索目录匹配模式,大多数程序使用明显的方式这些调用的文件。对于用C或C ++使用通常的工具程序,该扩展提供免费的C运行时库,使用Windows的API。

Unlike in *nix shells, replacement of a file pattern with the list of matching names is implemented within each command and not implemented by the shell itself. This can mean that different commands could implement different wild card pattern rules, but in practice this is quite rare as Windows provides API calls to search a directory for files that match a pattern and most programs use those calls in the obvious way. For programs written in C or C++ using the "usual" tools, that expansion is provided "for free" by the C runtime library, using the Windows API.

问题在Windows API是<一个href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418%28v=vs.85%29.aspx\"><$c$c>FindFirstFile()和它的近亲<一个href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/aa364419%28v=vs.85%29.aspx\"><$c$c>FindFirstFileEx(), <一href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/aa364428%28v=vs.85%29.aspx\"><$c$c>FindNextFile(),和<一个href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/aa364413%28v=vs.85%29.aspx\"><$c$c>FindClose().

The Windows API in question is FindFirstFile() and its close relatives FindFirstFileEx(), FindNextFile(), and FindClose().

奇怪的是,虽然的文档用FindFirstFile()描述其lpFileName的对象参数设置为目录或路径和文件名,其中可以包括通配符,例如,星号( * )或问号(),它从来没有真正定义是什么 * 字符的含义。

Oddly, although the documentation for FindFirstFile() describes its lpFileName parameter as "directory or path, and the file name, which can include wildcard characters, for example, an asterisk (*) or a question mark (?)" it never actually defines what the * and ? characters mean.

文件模式的确切含义有历史的 CP / M 操作系统的历史可以追溯到20世纪70年代初这极大地影响(也许有人会说被直接复制来代替影响在这里)MSDOS的设计。这导致了若干有趣的伪像和行为。其中的一些在光谱的DOS结束在是这样描述的博客文章从2007年的地方雷蒙德描述文件patters是究竟是如何在DOS下实现的。

The exact meaning of the file pattern has history in the CP/M operating system dating from the early 1970s that strongly influenced (some might say "was directly copied" in place of "influenced" here) the design of MSDOS. This has resulted in a number of "interesting" artifacts and behaviors. Some of this at the DOS end of the spectrum is described at this blog post from 2007 where Raymond describes exactly how file patters were implemented in DOS.

这篇关于奇怪的Windows DIR命令行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆