【发布时间】:2016-11-03 15:52:25
【问题描述】:
我在这里的一篇文章中找到了这行代码。我将它用于我的批处理文件,它可以工作。我只是想了解 FIND 命令后双引号中的“/”是什么意思。
dir /a-d "\\SERVERNAME\SHARE\FOLDER\*.ext" | find /C "/"
提前致谢!
【问题讨论】:
-
这是它需要找到的。
标签: windows batch-file find
我在这里的一篇文章中找到了这行代码。我将它用于我的批处理文件,它可以工作。我只是想了解 FIND 命令后双引号中的“/”是什么意思。
dir /a-d "\\SERVERNAME\SHARE\FOLDER\*.ext" | find /C "/"
提前致谢!
【问题讨论】:
标签: windows batch-file find
它告诉find 命令在重定向到find 的dir 命令中找到什么字符串。 /c 告诉 find 输出找到目标字符串的行数,因此如果您的日期分隔符是 /,则输出找到的文件数。
【讨论】:
如果你输入
FIND /?
在 CMD 窗口中,FIND 会这样告诉你它的用法:
FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]
/V Displays all lines NOT containing the specified string.
/C Displays only the count of lines containing the string.
/N Displays line numbers with the displayed lines.
/I Ignores the case of characters when searching for the string.
/OFF[LINE] Do not skip files with offline attribute set.
"string" Specifies the text string to find.
[drive:][path]filename
Specifies a file or files to search.
If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.
所以双引号中的字符串就是您希望FIND 找到的内容。
/? 选项受支持且对许多内置 DOS 命令很有用。
【讨论】:
命令行可能用于获取匹配文件的计数。由于dir 返回一个标题、一个包含日期/时间和大小的文件列表以及一个页脚,/ 可能已被选为搜索字符,因为这可能只出现在文件列表的日期中,而不是在额外的行(页眉/页脚)。这是dir /A-D "\\SERVERNAME\SHARE\FOLDER\*.ext" 的示例输出:
Volume in drive \\SERVERNAME\SHARE is DATA Volume Serial Number is 0000-0000 Directory of \\SERVERNAME\SHARE\FOLDER 03/11/2016 12:00 10 file.ext 03/11/2016 11:00 26 sample.ext 2 File(s) 36 bytes 0 Dir(s) 99,999,999,964 bytes free
如您所见,斜杠字符 (/) 仅出现在日期中,以防它被定义为当前机器上的日期分隔符,这使得整个方法依赖于区域设置。
管道| 将此输出传输到下一个命令,即find /C "/",该命令会计算至少包含一个/ 字符的行数(/C)。
如果我的怀疑是正确的,您可以改用以下行:
dir /B /A-D "\\SERVERNAME\SHARE\FOLDER\*.ext" | find /C /V ""
/B 开关告诉dir 返回一个没有任何额外内容的裸文件列表,例如:
sample.ext file.ext
然后find /C /V "" 命令计数 (/C) 所有不匹配 (/V) 空字符串 ("") 的行,因此它返回给定位置中 *.ext 文件的总数.与原始方法相反,这种方法完全独立于语言环境和区域设置。
【讨论】: