Listing all files in directories
Of course, every operating system that offers filesystem support also comes with some kind of utility that does just list all files within a directory in the filesystem. The simplest examples are the ls
command on Linux, MacOS, and other UNIX-related operating systems. In DOS and Windows, there is the dir
command. Both list all files in a directory and provide supplemental information such as file size, permissions, and so on.
Reimplementing such a tool is, however, also a nice standard task to get going with directory and file traversal. So, let's just do that!
Our own ls
/dir
utility will be able to list all items in a directory by name, indicate what kind of items there are, list their access permission flags, and display the number of bytes they occupy on the filesystem.
How to do it...
In this section, we will implement a little tool that lists all files in any user provided directory. It will not only list the filenames, but also their type, size, and access...