【发布时间】:2015-11-04 03:48:38
【问题描述】:
是否可以使用 find 函数搜索文件夹并返回具有任何字符串集合的文件的名称
例如,搜索文件夹并返回带有文本“Michael”、“Alan”、“Ben”等的文件。
【问题讨论】:
-
为什么关闭?这是一个合理的编程问题吗? dos shell 是一种脚本语言。
标签: dos
是否可以使用 find 函数搜索文件夹并返回具有任何字符串集合的文件的名称
例如,搜索文件夹并返回带有文本“Michael”、“Alan”、“Ben”等的文件。
【问题讨论】:
标签: dos
您可以在 DOS 和 Windows 中使用Findstr 命令。
FINDSTR [options] [/F:file] [/C:string] [/G:file]
[/D:DirList] [/A:color] [/OFF[LINE]] [string(s)] [pathname(s)]
Literal search
Search a text file mydir\*.* that contains the following
The quick brown fox The really ^brown^ fox
A literal search will ignore any special meaning for the search characters:
FINDSTR /C:"^brown" mydir\*.*
【讨论】:
我认为DOS没有FINDSTR,但如果你有,那么你可以使用下面的方法来查找内容包含“fox”或“dog”的文件的名称
findstr /ml "fox dog" "myDir\*"
或
findstr /m /c:"fox" /c:"dog"
但是有一个讨厌的 FINDSTR 错误,当您有多个不同长度的文字搜索字符串并且搜索区分大小写时,可能会导致丢失应该匹配的文件。由于“狗”和“狐狸”的长度相同,所以你不会有问题。但我怀疑你真正的搜索字符串的长度会有所不同。希望您可以摆脱不区分大小写的搜索,因为这样可以避免错误:
findstr /mli "string1 string2IsLonger" "myDir\*"
或
findstr /mi /c:"string1" /c:"string2IsLonger"
有关该错误的更多信息,请参阅Why doesn't this FINDSTR example with multiple literal search strings find a match?。我也推荐阅读What are the undocumented features and limitations of the Windows FINDSTR command?。
如果您的任何搜索字符串包含空格,那么您将需要使用/c 选项。
如果您必须搜索多个搜索字符串,那么我建议您使用/g:file 选项。
您可以从命令行输入HELP FINDSTR 或FINDSTR /? 以获取可用选项的完整列表。
【讨论】: