-2

我试图找到一个 Windows shell 命令,它可以让我选择所有以相同 3 个字符开头的文件。例如,如果一个目录具有以下内容:

000你好 000世界 111foo 121bar

该命令将为我提供前两个文件。有没有办法做到这一点?

4

3 回答 3

0

使用 bash 通配符

$ echo 000*
于 2012-05-07T20:11:40.900 回答
0

Windows 命令行,对吧?

dir 000*

*是与文件名中的任何字符匹配的通配符

?是与文件名中的单个字符匹配的通配符

根据您的新信息:

for /f %i in ('dir /b 000*') do (
   echo %i is the name of the file we found
   type %i
)

如果您在批处理文件中,请使用%%i.

这假设您希望留在当前工作目录中。如果您想将目录树遍历到子目录,请查看使用for /r.

于 2012-05-07T20:16:53.050 回答
0

由于它被标记为 Windows,因此不清楚需要哪种类型的解决方案,但如果可以使用 Ruby,这可能会奏效。

# create a hash of arrays where the hash value is the first three letters of the
# file name and the value is an array of those entries.
h = Hash.new{|h,k| h[k] = []}
Dir.foreach(".") { |f| h[f[0..2]] << f }

# then print/use the ones that have multiple entries
h.each_key { |k| puts h[k] if h[k].length > 1 }
于 2012-05-07T20:29:51.910 回答