7

Possible Duplicate:
Get names of all files from a folder with Ruby

I'm new to Ruby and I'm trying to get all the file names from a specific directory. There is only one level, and just need to get the entire list of names. How do I do that? I've looked at some of the other posts on the subject, but none helped.

Community
  • 1
  • 1
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156

4 Answers4

16

To list all the entries in the current directory:

Dir.entries('.')
pje
  • 21,801
  • 10
  • 54
  • 70
  • 1
    It's worth mentioning that this returns an array of strings that also contains `'.'` and `'..'`. – Niek Aug 19 '17 at 16:28
9
Dir.new('.').each {|file| puts file }

Note that this will include . and ..

mdeltito
  • 327
  • 1
  • 3
6

Seems OP asking about to list only files, not dirs as well.

Dir['path/to/dir/*'].select { |e| File.file?(e) }
4

You may use Dir#glob to return specific filenames for specific directory

Dir.glob("*") #Get all filenames on current directory
Dir.glob("somedirectory/*") #Get all filenames on some directory
Dir.glob("somedirectory/*.php") #Get specific files with specific extension on some directory
d3t0n4t0r
  • 94
  • 3