I am looking for a code that will find files without extensions. In Rails, there is a file app_name/doc/README_FOR_APP
. I am searching for a way to find files simular to this with no extension associated to the file, i.e., 'gemfile'. Something like:
我正在寻找一个代码,可以找到没有扩展名的文件。在Rails中,有一个文件app_name / doc / README_FOR_APP。我正在寻找一种方法来查找与此类似的文件,没有与文件关联的扩展名,即'gemfile'。就像是:
file = File.join(directory_path, "**", "__something__")
3 个解决方案
#1
4
Since your question didn't explicitly specify whether you want to search for files without extensions recursively (though in the comments it sounded like you might), or whether you would like to keep files with a leading dot (i.e. hidden files in unix), I'm including options for each scenario.
由于您的问题没有明确指定是否要以递归方式搜索没有扩展名的文件(尽管在评论中听起来像你可能),或者您是否希望保留带有前导点的文件(即unix中的隐藏文件),我为每个场景都包含了选项。
Visible Files (non-recursive)
可见文件(非递归)
Dir['*'].reject { |file| file.include?('.') }
will return an array of all files that do not contain a '.' and therefore only files that do not have extensions.
将返回一个不包含'。'的所有文件的数组。因此只有没有扩展名的文件。
Hidden Files (non-recursive)
隐藏文件(非递归)
Dir.new('.').entries.reject { |file| %w(. ..).include?(file) or file[1..-1].include?('.') }
This finds all of the files in the current directory and then removes any files with a '.' in any character except the first (i.e. any character from index 1 to the end, a.k.a index -1). Also note that since Dir.new('.').entries
contains '.' and '..' those are rejected as well.
这将找到当前目录中的所有文件,然后删除带有“。”的所有文件。在除第一个之外的任何字符中(即从索引1到结尾的任何字符,a.k.a index -1)。另请注意,因为Dir.new('。')。条目包含'。'和'..'那些也被拒绝了。
Visible Files (recursive)
可见文件(递归)
require 'find'
Find.find('.').reject { |file| File.basename(file).include?('.') }.map { |file| file[2..-1] }
The map on the end of this one is just to remain consistent with the others by removing the leading './'. If you don't care about that, you can remove it.
这个结尾处的地图只是通过删除前导'./'来保持与其他人的一致。如果您不关心它,可以将其删除。
Hidden Files (recursive)
隐藏文件(递归)
require 'find'
Find.find('.').reject { |file| File.basename(file)[1..-1].include?('.') }.map { |file| file[2..-1] }
Note: each of the above will also include directories (which are sometimes considered files too, well, in unix at least). To remove them, just add .select { |file| File.file?(file) }
to the end of any one of the above.
注意:上面的每一个都将包含目录(有时也被认为是文件,好吧,至少在unix中)。要删除它们,只需添加.select {| file | File.file?(file)}到上面任何一个结尾。
#2
1
Dir.glob(File.join(directory_path, "**", "*")).reject do |path|
File.directory?(path) || File.basename(path).include?('.')
end
Update: If you want to take a stricter definition of "extension", here's something a little more complex that considers a file name to have an extension if and only if it has exactly one dot and that dot is neither the first nor last character in the name:
更新:如果你想更严格地定义“扩展”,这里有一些更复杂的东西,它认为文件名具有扩展名,当且仅当它只有一个点并且该点既不是第一个也不是最后一个字符时名字:
Dir.glob(File.join(directory_path, "**", "*")).reject do |path|
name = File.basename(path)
File.directory?(path) || (name.count('.') == 1 && name[-1] != '.')
end
I suspect "not having a dot" is more what you were looking for, however.
不过,我怀疑“没有点”是你想要的更多。
#3
0
nonfile = File.join("**", "*.")
Dir.glob(nonfile).each do |path|
puts path
end
I was messing around and I was talking to a colleague and we thought if this. Wouldn't that do the trick?
我在乱搞,我正在和一位同事谈话,我们想是否这样。这不是诀窍吗?
#1
4
Since your question didn't explicitly specify whether you want to search for files without extensions recursively (though in the comments it sounded like you might), or whether you would like to keep files with a leading dot (i.e. hidden files in unix), I'm including options for each scenario.
由于您的问题没有明确指定是否要以递归方式搜索没有扩展名的文件(尽管在评论中听起来像你可能),或者您是否希望保留带有前导点的文件(即unix中的隐藏文件),我为每个场景都包含了选项。
Visible Files (non-recursive)
可见文件(非递归)
Dir['*'].reject { |file| file.include?('.') }
will return an array of all files that do not contain a '.' and therefore only files that do not have extensions.
将返回一个不包含'。'的所有文件的数组。因此只有没有扩展名的文件。
Hidden Files (non-recursive)
隐藏文件(非递归)
Dir.new('.').entries.reject { |file| %w(. ..).include?(file) or file[1..-1].include?('.') }
This finds all of the files in the current directory and then removes any files with a '.' in any character except the first (i.e. any character from index 1 to the end, a.k.a index -1). Also note that since Dir.new('.').entries
contains '.' and '..' those are rejected as well.
这将找到当前目录中的所有文件,然后删除带有“。”的所有文件。在除第一个之外的任何字符中(即从索引1到结尾的任何字符,a.k.a index -1)。另请注意,因为Dir.new('。')。条目包含'。'和'..'那些也被拒绝了。
Visible Files (recursive)
可见文件(递归)
require 'find'
Find.find('.').reject { |file| File.basename(file).include?('.') }.map { |file| file[2..-1] }
The map on the end of this one is just to remain consistent with the others by removing the leading './'. If you don't care about that, you can remove it.
这个结尾处的地图只是通过删除前导'./'来保持与其他人的一致。如果您不关心它,可以将其删除。
Hidden Files (recursive)
隐藏文件(递归)
require 'find'
Find.find('.').reject { |file| File.basename(file)[1..-1].include?('.') }.map { |file| file[2..-1] }
Note: each of the above will also include directories (which are sometimes considered files too, well, in unix at least). To remove them, just add .select { |file| File.file?(file) }
to the end of any one of the above.
注意:上面的每一个都将包含目录(有时也被认为是文件,好吧,至少在unix中)。要删除它们,只需添加.select {| file | File.file?(file)}到上面任何一个结尾。
#2
1
Dir.glob(File.join(directory_path, "**", "*")).reject do |path|
File.directory?(path) || File.basename(path).include?('.')
end
Update: If you want to take a stricter definition of "extension", here's something a little more complex that considers a file name to have an extension if and only if it has exactly one dot and that dot is neither the first nor last character in the name:
更新:如果你想更严格地定义“扩展”,这里有一些更复杂的东西,它认为文件名具有扩展名,当且仅当它只有一个点并且该点既不是第一个也不是最后一个字符时名字:
Dir.glob(File.join(directory_path, "**", "*")).reject do |path|
name = File.basename(path)
File.directory?(path) || (name.count('.') == 1 && name[-1] != '.')
end
I suspect "not having a dot" is more what you were looking for, however.
不过,我怀疑“没有点”是你想要的更多。
#3
0
nonfile = File.join("**", "*.")
Dir.glob(nonfile).each do |path|
puts path
end
I was messing around and I was talking to a colleague and we thought if this. Wouldn't that do the trick?
我在乱搞,我正在和一位同事谈话,我们想是否这样。这不是诀窍吗?