Command line tip #3 - Targeted deleting

The Windows XP (and Vista) command shell has a delete command “del” that provides flexibility and power for the chore of deleting files. If there are any voices in the background saying, “What else is new, it’s just DOS”, they couldn’t be more wrong. This 32-bit command shell is much more powerful. The 16-bit DOS commands could not recognize long file names and lacked many features that the newer shell has. The 32-bit “del” command can be much more specific about what you wish to delete. Let’s consider some useful scenarios.

Scenario 1 - You want to delete every file with a certain name.

I just posted about getting rid of Thumbs.db and this is an excellent case where we can demonstrate the power of the command line. To delete every Thumbs.db file in every folder on the C: drive (except certain virtual folders), enter the one-line command:
del /s /a:s C:\thumbs.db

That’s all that’s required. What could be easier? The parts of the command “/s” and “/a:s” are what are called switches or, sometimes, flags. Switches vary from command to command and they allow a command to be modified or configured. In the case of “del”, the switch “/s” applies the delete command to all subfolders; the switch “/a:s” applies the delete command to files with the “system” attribute. To get more information about what switches are available enter the commanddel /?

Scenario 2 - You want to delete every file of a given type.

Here we use the feature that lets the delete command use the wild-card operator “*”. To remove all files from the C: drive that have extension .tmp we can use the command:del /s /f /q C:\*.tmp

There are two more switches used in this command. The switch “/f” forces the deletion of read-only files and the switch “/q” runs the command in “quiet” mode. When using a wildcard, Windows will keep asking you to confirm deletions if you do not use quiet mode. Note that the wildcard can be used in various ways, not just to remove all files with a particular extension.

Please remember that deletions done in the command line do not get sent to the Recycle Bin.

In the next tip, I will be showing how to use the command line to make lists of useful information. Also, there are other tips on using the command line at commandwindows.com.

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Sorry, the comment form is closed at this time.