Introduction.
The Dir
Command, originally part of the Disk Operating System, is also available in Microsoft Access VBA. While it is commonly used to check the presence of files or folders on a disk, you can also use it to generate a complete listing of all folders and files, along with their full pathnames. For example, you can retrieve paths in the format 'C:\Folder\Subfolder\Subfolder\...
or C:\Folder\Subfolder\FileName'
. Such a listing is useful for reviewing disk usage, organizing files, or maintaining records for future reference.
In 1996–97, during our organization’s migration from Novell NetWare to the Windows NT system, all user departments were instructed to review their server folder structures and remove any unused or unnecessary files and folders before the transition. This requirement led me to take a closer look at the DIR command. By using a combination of optional parameters, I was able to generate a complete listing of all folders and subfolders on our department’s server, accessed through the mapped server drive on a Windows 95 client machine. This listing proved invaluable in reviewing the contents and identifying obsolete folders and files for removal before migration.
You can generate a folder listing using either the DIR
command or the TREE
command, each producing a different style of output. Personally, I prefer the DIR
command. The TREE
Command creates a graphical view, displaying the hierarchical structure of folders and subfolders. In contrast, the DIR
command presents each folder and subfolder on a single line, separated by backslashes. A sample of both listings.
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 the 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 the 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 its 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 the 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.
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 a File.
By default, the output of a DOS command is displayed directly on the screen. If you need a printed copy, the output must first be saved to a text file. This can be done using the output redirection symbol (>) followed by the desired file name. For example, the following command saves the output to a file named FolderList.txt:
C:/>DIR "\RADIO" /A:D/S/B > FolderList.txt
Note: If you are generating a listing of all folders and subfolders/files on a disk, the process may take some time to complete and save all the details to a text file. During this period, it may appear as though the computer has hung. Be patient and wait for the DOS prompt (C:\>) to reappear.
If you wish to terminate the command before it finishes, you can press Ctrl + C.
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
| (Vertical Bar) – This symbol is called the piping symbol. It directs the output of one command to another command for further processing.
For instance, when used with the Type command, the piping symbol passes its output to the More command. The More command displays the output one screen at a time, similar to using the /P parameter with the DIR command. Press any key to view the next page of output.
The TREE Command.
The TREE command displays the folder list in a hierarchical structure.
C:/>TREE | More
Display the folder structure listing page-wise.
C:/>TREE/F | More
/F parameter displays folder names followed by Filenames.
Hope you have enjoyed doing something different and useful.
No comments:
Post a Comment
Comments subject to moderation before publishing.