We often have to extract the list of files in a given directory, possibly sorting them according to a given criteria and applying filters to include in the list only the necessary files and exclude others.
To do that DOS provides us with the famous dir command that offers some options that can help us to accurately extrapolate what we want.
In this case we want to create a file containing the list of files in the current folder, with the following constraints:
- In the list there must be only the file name and not the path where it is located
- The list must contain only files, of any type, and not the directories
- The files in the list must be sorted from newest to oldest
- The result must be saved in a file called “listaFile.txt”
Starting from the content of the folder displayed above, the “listaFile.txt” file must then have the following content:
Let’s start by running the dir command without any options:
Looking a the output of the dir commands with no options there are several things we need to fix. First, we should eliminate all unnecessary information, such as the description of the unit type, the details about date and time related to the files and the final summary information.
To do this we need to invoke the dir command with the /B option that means Bare format , ie a format that precisely not include header, size, summary, etc ..
So, let’s try to run the command: dir /B
Well, we have removed the additional information we don’t need and we now have the list of files without their full path. However, we can notice that the result produced by the command also lists the directories and not just the file as it was required. We must therefore find a way to indicate to the dir command that we want to exclude them.
Also this requirement can be easily satisfied through command options; in particular the /A option of the command allows us to specify the attributes that files need to have to be taken into account.
These attributes can for example be:
D for Directories
R for Read-only files
H for Hidden files
S System files
The attributes in combination with the /A option allow us to select only the files that have certain characteristics. For example, with the /AD option we can list only directories. But we must do exactly the opposite, so do not select items that have a given attribute, but eliminate instead those who possess it. In this case the D attribute that indicates that the entry is a directories. To do that we have to preface to the attribute the symbol – (hyphen) to indicate that we want all the files “less” those that have a certain attribute (in this case, the D directories).
Let’s run the command: dir /B /A-D
Ok, we have succeeded and directories “Folder 1” and “Folder 2” have disappeared from the list. We are close to the solution to our request and we have just to meet the last clause, one that asks us to list the files from newest to oldest.
The solution, again, is simply between the dir command options. The /O option, in fact, allows us to specify a sort criteria for the items listed in the command output.
The possible sorting criteria are:
N by Name (in alphabetical order)
S by Size (with smallest first)
E by Extension (in alphabetical order)
D by Date (with oldest first)
G by Group (with directories first)
Again, we can use the hyphen symbol – to reverse the order criteria defined by default. Returning to our example, we are required to sort the files by date, so we will use the option D . But by default, the D option puts the older files at the top of the list, while we need the opposite, with newest first. We must therefore use the – to reverse the behavior:
So, our command becames: dir /B /A-D /O-D
The files, as we can see looking at the first figure of the article, are listed by date in order from newest to oldest, so we finally got the desired result.
At this point the last thing to do is to save this list on a new file. We then use the redirection operator ‘ > ‘ to indicate that we want to direct the output of the command on the file “listaFiles.txt”.
So, let’s run: dir /B /A-D /O-D > listaFiles.txt
In this case, as mentioned, we redirected the command output to a file, so we do not see any messages in the console and we just get again the prompt.
We go to look in our folder and we can see it has been created the “listaFiles.txt” file that will contain the result of the executed command and, therefore, the list of our files.
We then open the file and we go to check the contents:
Wait a minute, there’s something wrong. The file contains the list of files available in the folder, but the list also included himself, having it been generated within the folder itself. And it is also the first on the list, being the last to be created and so the most recent.
To avoid having the output file included in the list we don’t create it in the same folder we want to list the contents but, for example, in a folder at the previous level.
Going to check again the contents of the “listFiles.txt” file we see that this time it is correct:
Now let’s try to add an additional condition to select files to be shown:
– We want to list only files with .docx extension
Actually this condition does not add great complexity and we can simply satisfy it by using the wildcard ‘ * ‘ as follows:
As we can see, we have limited the search only to .docx files
In case we would instead filter the list of files using multiple conditions, such as 2 or more different extensions, but in general any rule on the naming of the files, we can use the findstr command in conjunction with dir . To do this we use the operator ‘ | ‘ (pipe) that directs the output of the dir command as input for the findstr command.
In the following example we select all the .docx and .xslx files
L’opzione /I del comando findstr sta ad indicare di effettuare il confronto in modalità case insensibile, cioè senza distinzione tra maiuscole e minuscole.
The /I option of the findstr command indicates to use the case insensitive mode for the comparison.