How can I return a list of only the files, not directories, in a specified directory?
I have my_list = Dir.glob(script_path.join("*"))
This returns everything in the directory,including subdirectories. I searched but haven't been able to find the answer.
listdir() – It is used to list the directory contents. The path of directory is passed as an argument.
In addition to Mark's answer, Dir.entries
will give back directories. If you just want the files, you can test each entry to see if it's a file or a directory, by using file?
.
Dir.entries('/home/theiv').select { |f| File.file?(f) }
Replace /home/theiv
with whatever directory you want to look for files in.
Also, have a look at File. It provides a bunch of tests and properties you can retrieve about files.
Dir.glob('*').select { |fn| File.file?(fn) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With