开发者

Listing the files in a directory and all subdirectory

开发者 https://www.devze.com 2023-03-30 06:01 出处:网络
Is there any class in ruby for开发者_开发技巧 listing all the files in a directory and all the files in the subdirectory?You might look at Dir.glob. You can pass it the **/* path which will give you e

Is there any class in ruby for开发者_开发技巧 listing all the files in a directory and all the files in the subdirectory?


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) }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号