Iterate over directory
Crystal
Similar to ruby Dir.
Work with pattern scan, but currently ** not supported ⚠recursive issue
Dir["/app/**"].each {|filename|
puts filename
}
Ruby / SO
Dir["**/*"].each do |filename|
puts filename
end
see also
- How to list directory contents including dotfiles but not . and ..?
- Paths in Ruby - for path manipulation, eg:
File.expand_path( File.join( File.dirname(__FILE__), '../other' ))
orFile.expand_path('../other', __FILE__)
C++ (x17)
#include <filesystem>
namespace fs = std::filesystem;
int main() {
for(auto& p: fs::directory_iterator("/app"))
std::cout << p.path() << '\n';
}
for g++ version < 9.0 link with -lstdc++fs
see undefined reference when using experimental/filesystem
see also
- std::filesystem::is_directory - example of checking different filetype. There are 2 kind of api: one trigger exception, the other return an error code.
Written on August 14, 2020, Last update on June 8, 2024
loop
iterate
filesystem
file
ruby
crystal
c++