Is there any class in ruby for listing all the files in a directory and all the files in the subdirectory?
1 Answer
You might look at Dir.glob
. You can pass it the **/*
path which will give you everything in the folder and its subdirectories:
records = Dir.glob("path/to/your/root/directory/**/*")
# Will return everything - files and folders - from the root level of your root directory and all it's subfolders
# => ["file1.txt", "file2.txt", "dir1", "dir1/file1.txt", ...]
Since you probably want a list of files, excluding folders, you can use:
records = Dir.glob("path/to/your/root/directory/**/*").reject { |f| File.directory?(f) }
-
If I want list everything in special directory not current directory , what can I do ? Aug 24, 2011 at 20:56
-
4
-
1
-
6For future googlers, to get only files: Dir.glob("*/").reject {|f| File.directory?(f)} Nov 21, 2018 at 20:37