7. The 'DEL' command and Glob patterns
Use the 'DEL' command to delete data. Warning: handle the 'DEL' command with care. And you should use Glob patterns cautiously with the 'DEL' command. In special cases, the shell will ask you to confirm a deletion. But do not rely on this!
Tip: sometimes you could use the switch '/P' that prompts you for confirmation before deleting a file. Of course, this is not what you want
when deleting many files.
7.1 Delete files form the current directory
To delete or erase 'file1.txt', 'file2.dat', 'file2.txt' and 'file3.txt' in our example directory D:\test, we could type:
D:\test>DEL file?.*
(as we saw earlier)
Another example: the command
D:\test>DEL *.jpg [ENTER]
will delete all image files with extension '.jpg' from our example directory.
To delete also files with the attribute read-only, you have to add the switch '/F' as a parameter to the 'DEL' command.
D: est>DEL /F *.jpg [ENTER]
7.2 Delete files in the subdirectory 'my Doc' -1
The command
D:\test>CD "my Doc" [ENTER]
results in the prompt
D:\test\my Doc>
To erase all files in this directory, type
D:\test\my Doc>DEL * [ENTER]
The character * refers to all files in the subdirectory 'my Doc'.
7.3 Delete files in the subdirectory 'my Doc' -2
The steps in the last example 'CD "my Doc"' and 'DEL *' could be combined into one line of code:
D: est>CD "my Doc" & DEL * [ENTER]
or shorter
D: est>CD m* & DEL * [ENTER]
where 'm*' refers to the subdirectory 'my Doc'.
Notice that this code does not work if another subdirectory with the first letter 'm' exists, e.g. myBackup.
7.3.1 An alternative
The last examples consisted of several steps, that are combined. In case of 'DEL', there is an easier alternative:
D:\test>DEL "my Doc"\* [ENTER]
or still shorter
D:\test>DEL "my Doc" [ENTER]
Notice that the subdirectory will not be erased; only the files will be deleted.
D:\test>RD "my Doc" /S [ENTER]
will erase the subdirectory 'my Doc' and all its files ('RD' is an abbreviation of Remove Directory; for also deleting subdirectories, the switch '/S' is necessary).
7.3.2 ROBOCOPY
'ROBOCOPY' is very efficient in deleting files and (sub)directories.
With the switches '/MOV /S /E' you copy the files from the source to the destination and after copying you delete the source files.
D:\test>ROBOCOPY /MOV /S /E D:\test D:\test_backup [ENTER]
With the '/MOVE' switch, you can delete both files and directories. Be careful with this command!
Before studying the 'REN' function in depth (chapter 9), which will show you the power of the command line,
we first have a closer look at the combining of commands.