Use the right API for iterating a directory
Use DirEntries instead of constructing paths and request data randomly makes the script a bit faster and the code easier to read.