Ruby find all files in a directory recursively
Improve this question. Add a comment. Active Oldest Votes. Try this: Dir. Improve this answer. I think the OP wanted recursive, didn't they? Answer should actually be Dir. Also case insensitive by default — leifg. Konstantin This, or Dir [] , are what I usually use. However, there is a catch: Dir.
This is usually fine, but if you have a great number of paths, one may prefer to use the Find module instead, since it delivers paths to the block as it finds them. I agree with WayneConrad on this. You can inadvertently halt your program as Ruby allocates enough memory to store a big array. This is very similar to slurping a file. It's more efficient, and probably faster, to let Find process the hierarchy rather than throw it at the OS and potentially get an unexpected array.
Debugging that situation is difficult. Show 3 more comments. AutonomousApps 3, 2 2 gold badges 29 29 silver badges 40 40 bronze badges. If speed is a concern, prefer Dir. Warming up Find. Wayne Vucenic 27 5 5 bronze badges. Dennis Dennis 50k 25 25 gold badges silver badges bronze badges. Personally, I found this the most useful for looping over files in a folder, forward looking safety:. The following snippets exactly shows the name of the files inside a directory, skipping subdirectories and ".
Then, we have to provide a full path of the file to File. That's why this File. Now, Dir. While getting all the file names in a directory, this snippet can be used to reject both directories [. After choosing a path, I need to figure out how to loop through files. Or is there an easier way I'm missing? Example of direc structure:. Accepted Answer. Guillaume on 14 Nov Vote 6.
Cancel Copy to Clipboard. So it's as simple as:. KAE on 22 Jan So glad to know dir is recursive! Daniel Poiesz on 9 Aug More Answers 0. See Also. Tags directory file processing.
Start Hunting!
0コメント