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

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++