How do I find files in a directory that do not have extensions?
ruby
Solution
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.
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.
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.
Problem
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: ``` file = File.join(directory_path, "**", "__something__") ```