Is there any class in ruby for listing all the files in a directory and all the files in the subdirectory?
The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.
By default, ls lists just one directory. If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively.
Use the ls command to display the contents of a directory. The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags.
Substitute dir /A:D. /B /S > FolderList. txt to produce a list of all folders and all subfolders of the directory.
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 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