Introduction.
Dir Disk Operating System (DOS) Command is available in Microsoft Access VBA too. The usage of this command in VBA is for checking the presence of Files / Folders on the Disk. But, do you ever thought of taking a listing of all the folders/files with their complete pathname, starting from its root (C:\folder\folder\folder.., or C:\folder\folder\folder\Filename), for future reference or simply for reviewing your disk usage and maintenance.
Way back in 1996-97, when it was time to change our Corporate LAN Software from Novell Netware to Windows NT System all the User Departments are asked to review their Server folder structure and advised to remove unused or unwanted folders/files so that old Server contents can be migrated to the new Windows NT Server. This requirement encouraged me to take a closer look at the DIR command. With the help of combining a few optional parameters of the command, I was able to prepare a listing of all the folders and sub-folders of our department Server, taken from Windows 95 Client Machine’s mapped Server Drive. This listing helped a lot to review the folders and remove obsolete folders/files.
You can take a listing of folders using either the DIR command or the TREE command. Both commands produce different styles of listings. I prefer the DIR command. The TREE command creates a graphical listing, showing the hierarchical placement of each folder. DIR command displays all the folders and sub-folder(s) separated with back-slashes in one line. A sample listing of both types is given below, one after the other:
DIR Command has several optional parameters to prepare listings in different ways depending on your requirements. Most of the time we ignore these options because their usage is not common.
DOS Command Help.
You can get a list of all optional parameters with a simple help command parameter (/?). The Usage is as given below. First, let us open the DOS Command Window.
- Right-click on Windows Start Button and click on the Run command.
- Type cmd in the Open control and click OK to open the DOS Command Prompt.
- If the prompt appears as something like C:\Users\User> then Type:
Cd \ then press Enter key to set the prompt to C:\>
Cd stands for Change Directory command. The \ is the name of the Root Folder. This will set the Disk Drive C root folder as current.
Tip: Type the Command Exit and press the Enter key to close the DOS Window, any time you want.
- Type the following command to display a list of optional parameters of the DIR command:
DIR /?
You can display the details of any DOS command and their usage in this way, by typing the command followed by /?, in the DOS Command prompt.
Display Folders in C: Drive.
Now, let us display the listing of all the folders in C: drive on the screen.
Warning: Don't say I didn't warn you that this will be a lengthy list, and may take a few minutes to display all of them on the screen.
Tip: You may terminate the listing at any point by pressing Ctrl+C Keys.
Type the following command in the DOS Prompt and press Enter Key.
C:/>DIR /A:D/S/B/P
Let us take a look at each parameter given with the DIR command.
DIR Command and its few Options.
- /A - Display files with specific Attributes. Specific attributes are given, separated by a colon like /A:D D - for directories.
- /S - include Sub-folders also in the listing.
- /B - take a Bare-format listing and exclude summary information.
- /P - display the listing Page-wise (Pause the listing when a screen full of information is displayed. Press any key to display the next page).
If you need a listing of a particular folder and its sub-folders only then include the folder name in the command as given below:
C:/>DIR "\RADIO" /A:D/S/B/P
Saving the DIRectory Listing to File.
By default, DOS Command's output is directly sent to the screen for viewing. If you need a printout on paper, then we must save the output into a text file. This method uses an output re-direction symbol (>) followed by a text file name. The following command saves the output to the FolderList.txt file.
C:/>DIR "\RADIO" /A:D/S/B > FolderList.txt
NB: If you are taking a listing of all the folders and sub-folders/files of a disk then it will take some time to save all the details in the text file and complete the command. It may look like the computer hanged, have patience and wait for the DOS prompt C:/> to re-appear. If you run out of patience and would like to terminate the command then press Ctrl+C Keys.
You may open the text file FolderList.txt in any plain text editor program to take printouts.
The TYPE of Command.
You may use the following DOS command to display the contents of the text file:
Type FolderList.txt | More
Type
| (The vertical bar) this symbol is known as Piping Symbol. The piping symbol ensures that the output of the Type command is passed to the next command More. The More command displays the output one screen-full at a time, like the /P parameter to the DIR command. Press a key to display the next page.
The TREE Command.
The TREE command displays the folder list in a hierarchical structure.
C:/>TREE | More
Display folder structure listing page-wise.
C:/>TREE/F | More
/F parameter display folder names followed by Filenames.
Hope you have enjoyed doing something different and useful.