How to List File in Folder in Ruby

Stewart Nguyen Feb 02, 2024
  1. Create a Sample Folder
  2. Use Dir::[] to List All Files in a Folder in Ruby
  3. Use Dir.glob to List All Files in a Folder in Ruby
  4. Use Dir.entries to List All Files in a Folder in Ruby
  5. Use Pathname to List All Files in a Folder in Ruby
  6. Conclusion
How to List File in Folder in Ruby

Listing all files in a specific folder can be essential for various tasks in Ruby programming. In this guide, we’ll explore different methods to accomplish this task.

We’ll create a sample folder structure for demonstration purposes and then showcase the following methods: using Dir::[], Dir.glob, Dir.entries, and Pathname.

Create a Sample Folder

Before we dive into the code examples, let’s set up a sample folder structure in your current directory.

Command:

mkdir parent_folder
touch parent_folder/music_1.txt
touch parent_folder/music_2.txt

mkdir parent_folder/child_folder
touch parent_folder/child_folder/doc_1.txt
touch parent_folder/child_folder/doc_2.txt

We’ve created a parent_folder with two files (music_1.txt and music_2.txt) and a subfolder child_folder containing two more files (doc_1.txt and doc_2.txt).

Use Dir::[] to List All Files in a Folder in Ruby

The Dir::[] method returns an array containing all file and folder names inside the specified directory. To list only files, we can apply the File.file? method to filter the results.

In the syntax below, the Dir['directory_path/*'] uses the Dir::[] method to list all items (files and directories) in the specified directory ('directory_path') and the wildcard * is used to match all items in the directory.

Syntax:

items = Dir['directory_path/*']

Example:

files = Dir['parent_folder/*'].select { |path| File.file?(path) }
puts files

The code starts by using the Dir class to list all items (files and directories) in the 'parent_folder' directory. The Dir['parent_folder/*'] expression returns an array of file and directory paths inside the 'parent_folder'.

The .select method is then called on the array of paths returned by Dir['parent_folder/*']. Inside the block of .select, each path is checked using File.file?(path) to determine if it’s a file (as opposed to a directory or other types of items).

Only the file paths are selected and stored in the files variable. Finally, the code prints the contents of the files variable using puts, which displays a list of file paths found within the 'parent_folder' directory.

Output:

parent_folder/music_1.txt
parent_folder/music_2.txt

The output lists the files located in the "parent_folder". It displays the full paths of the files, with each file on a new line.

To list files recursively inside subfolders, you can use the following pattern.

Example:

files = Dir['parent_folder/**/*'].select { |path| File.file?(path) }
puts files

Output:

parent_folder/music_1.txt
parent_folder/music_2.txt
parent_folder/child_folder/doc_2.txt
parent_folder/child_folder/doc_1.txt

The output lists all the files located in both the "parent_folder" and its subfolder "child_folder", including their respective paths.

Use Dir.glob to List All Files in a Folder in Ruby

The Dir.glob method is equivalent to using Dir::[] and allows us to list files in a folder. It also supports recursive listing.

In the syntax below, the Dir.glob('parent_folder/**/*') uses the Dir.glob method to list all items (files and directories) in the 'parent_folder' directory and its subdirectories and the pattern 'parent_folder/**/*' is used to match all files and subdirectories recursively.

Syntax:

files = Dir.glob('parent_folder/**/*')

Example:

files = Dir.glob('parent_folder/**/*').select { |path| File.file?(path) }
puts files

The code uses the Dir.glob method to recursively list all items (files and directories) within the 'parent_folder' directory and its subdirectories. The pattern 'parent_folder/**/*' is used to match all files and subdirectories under 'parent_folder'.

The .select method is then called on the array of paths returned by Dir.glob. Inside the block of .select, each 'path' is checked using File.file?(path) to determine if it’s a file (as opposed to a directory or other types of items).

Only the file paths are selected and stored in the files variable. Lastly, it prints the contents of the files variable.

Output:

parent_folder/music_1.txt
parent_folder/music_2.txt
parent_folder/child_folder/doc_2.txt
parent_folder/child_folder/doc_1.txt

The output is the same as the previous example, listing all the files located in both the "parent_folder" and its subfolder "child_folder", including their respective paths.

Use Dir.entries to List All Files in a Folder in Ruby

The Dir.entries method can be used to list all entries (both files and folders) in a directory. To list only files, you can filter the results using the File.file? method.

In the syntax below, the folder_path = 'parent_folder' defines a variable called folder_path and assigns it the path to the directory you want to work with, which is 'parent_folder'. The next line, entries = Dir.entries(folder_path), uses the Dir.entries method is used to list all entries (files and directories) in the specified directory ('parent_folder').

The results are stored in the entries variable as an array of strings.

Syntax:

folder_path = 'parent_folder'
entries = Dir.entries(folder_path)

Example:

folder_path = 'parent_folder'
entries = Dir.entries(folder_path)
files = entries.select { |entry| File.file?(File.join(folder_path, entry)) }
puts files

The code starts by defining the folder_path variable that contains the path to the directory you want to work with. It uses the Dir.entries method to list all entries (files and directories) in the specified directory ('parent_folder' in this case) and the results are stored in the entries variable as an array of strings.

The .select method is then called on the entries array to filter out only the entries that are files. Inside the block of .select, each entry is checked using File.file?(File.join(folder_path, entry)).

The File.join method is used to create the full path to the entry within folder_path. Only the file names (not the full paths) of the selected files are stored in the files variable.

Finally, the code prints the contents of the files.

Output:

music_1.txt
music_2.txt

The output displays the file names only without the path to the "parent_folder".

Use Pathname to List All Files in a Folder in Ruby

The Pathname class provides a more object-oriented way to work with file and directory paths. You can use Pathname to list files in a folder and its subfolders.

In the syntax below, the line require 'pathname' imports the Pathname library, which provides a convenient way to work with file paths. In the next line, folder_path = 'parent_folder'; the folder_path variable is defined, containing the path to the directory you want to work with.

The path = Pathname.new(folder_path) line creates a Pathname object called path representing the specified directory.

Syntax:

require 'pathname'
folder_path = 'parent_folder'
path = Pathname.new(folder_path)

Example:

require 'pathname'

folder_path = 'parent_folder'
path = Pathname.new(folder_path)

path.find do |entry|
  puts entry if entry.file?
end

In the code above, we include the 'pathname' library to work with file paths. We define the folder path (folder_path = 'parent_folder') we want to explore.

Then, we create a Pathname object called path representing the 'parent_folder'. Next, we use the find method to traverse the directory and its subdirectories.

Lastly, we check if entry is a file, and if so, we print its path.

Output:

parent_folder/music_1.txt
parent_folder/music_2.txt
parent_folder/child_folder/doc_2.txt
parent_folder/child_folder/doc_1.txt

In the code, it uses the Pathname library to list all files located in both the "parent_folder" and its subfolder "child_folder", including their respective paths.

Conclusion

Listing files in a folder is a common operation in Ruby programming. Choose the method that best suits your needs and coding style.

Whether you prefer the simplicity of Dir::[] and Dir.glob, the flexibility of Dir.entries, or the object-oriented nature of Pathname, Ruby provides options to efficiently list files, making your file-related tasks more manageable.

Related Article - Ruby File