Windows provides the command-line interface (cmd) for various operations where one of them is deleting files. Windows provides del, rm commands in order to delete specified file or files from the command line interface with various options. In the tutorial, we will learn how to delete a file or files from command-line interface (cmd) with different commands in various ways.
Delete File with del Command
The del command is very old command provided with the first versions of the MS-DOS. the del command can be used to delete file or files via cmd in different ways.
Delete Single File
We will start with a simple example. The del command can be used to delete or remove a single file via the command line interface.
del myfile.txt
If the file name contains space or special characters using the double quoto is a safe way to prevent errors based file name.
del "my file.txt"
The most reliable way to delete a single file is provide its absolute path or full path inside the double quotos like below.
del "C:\data\my file.txt"
Delete Multiple Files
The del command can be used to delete multiple files. Just provide the file names by separating them spaces. We can see that by using double quotes multiple files with file names containing space can be easily deleted.
del "my file.txt" "yourfile.txt" "1file.txt"
Delete Specific File Extension
File extension are used to specify and display file content and file type. also the del command can be used to delete or remove specific file types according to their file extensions. For example “*.txt” is used specify text files.
del "*.txt"
We can also specify the absolute path or full path where all files with the specifed extension will be removed.
del "D:\data\*.txt"
Delete Specified File Names
Similar to the file extension different file name patterns can be specified for the delete or remove operation. The glob “*” can be used to express different file names. In the following example, we will delete all files whose names start with “A”.
del "A*"
Delete Read Only File
Read-only files are cannot be deleted by default. But the del command provides the /F or /a:R option in order to delete read-only files.
del /F "*"