Ruby program to get the list the name of files in a specified directory

Ruby Example: Write a program to get the list the name of files in a specified directory.
Submitted by Nidhi, on February 20, 2022

Problem Solution:

In this program, we will get the list of filenames in the specified directory and print the result.

Program/Source Code:

The source code to list the name of files in the specified directory is given below. The given program is compiled and executed on Windows 10 Operating System successfully.

# Ruby program to list the name of files 
# in a specified directory

filenames = Dir::entries("MyDir");

puts "File names: ",filenames;

Output:

File names:
.
..
SampleFile1.txt
SampleFile2.txt
SampleFile3.txt
SampleFile4.txt

Explanation:

In the above program, we get the list of filenames in the MyDir directory using the Dir::entries() method and print the result.

Ruby Directories Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.