Do you want to delete the files from Windows command prompt? Windows provides DEL
or ERASE
command to delete the files from the File System. We can use these commands either in the scripts or we can directly use these from the command prompt.
Through this article we are going to discuss how to use DEL command to delete the files from Windows File System.
DEL
or ERASE
command
This command is used to delete the files from the Windows File System. It allows to delete a single file or multiple files. You have to be more cautious when you are deleting the files. You will loose lot of useful information when you accidentally delete the files and also it is difficult to retrieve the files once they are erased.
ERASE
or DEL
command provides the switches to control the way deleting of the files.
Delete a file or multiple files
To delete a file, pass the path of the file to the DEL
command. The path is the relative or fully qualified path. For example, to delete a file “sample.txt”, you can use the below command.
C:\> del sample.txt
If the file doesn’t exists, Windows will throw below message.
Could Not Find c:\sample.txt
Above command, deletes only a single file. How to delete multiple files? You can pass the multiple files through the command, separating each file either by a comma (“,”) or a blank space (” “).
C:\> erase sample.txt, anothersample.txt
Delete all files from the folder
We can not delete the folder using these commands. But we can delete all the content of the folder. To delete all files from the folder, you can give the folder name as an argument. For example, to delete all files from the folder you can use below command.
C:\> erase Personal C:\Personal\*, Are you sure (Y/N)?
Observe that, when you delete the files from the folder it prompts to get the confirmation before delete the files.
Prompt before you delete the file(s)
By default, these commands deletes the files without prompting the user to delete them. But it is required sometimes to delete the files with the permission from the user. DEL
command provides “/P” switch to prompt the user to delete the files.
C:\> del sample.txt /p
If you have multiple files to delete; the above command prompts to get the confirmation from the User to delete each file.
Delete files using wildcards
DEL
command supports wildcards to delete multiple files at a time. We can use the wildcards “?” and “*” to delete the files. “?” represents the single character and “*” represents all characters in the file name.
To delete all the files with the extension “.txt”, we can write the command like below.
C:\> del *.txt
Be on learning path always!
// Malin