Script examples to learn how to delete Windows files using the cmd prompt and the dir command.
To delete Windows files using a cmd script, it is recommended to use the del command. The del or delete command provides many very useful options to be in control of the deletion process. The Command Prompt is a text-based user interface that enables users to execute commands and perform scripted tasks with their computer.
It’s a powerful tool that can be used in a variety of ways. Users can use the Windows command prompt to recursively copy files, recursively delete files, manage the registry and much more.
Table of Contents
1. Benefits of deleting files using the command prompt
Deleting files using the command prompt can provide several benefits over other methods. Firstly, it allows for faster deletion of multiple files or directories with a single command, reducing the time needed to perform the task. Additionally, the command prompt can be used to delete files that are otherwise difficult to delete using other methods.
For example, files with long file names, special characters or those that are locked by other applications can be deleted using the command prompt. Finally, using the command prompt can be more efficient for advanced users who are familiar with command line interfaces and prefer to use them over graphical user interfaces.
In this article, we will show you how to delete files with the Windows command prompt and why you should start using it. It’s also an easy way to execute commands without using the graphical user interface (GUI). The cmd is a part of the administration commands available in MS systems. To delete a file with cmd, you need to provide the exact path of the file.
If you don’t know where the file is located, you can use the dir command to locate it. The main option that we suggest using is the deletion confirmation, which allows you to control one last time which file to delete. However, please note that the files deleted with the del command line are not stored in the Recycle Bin, so you cannot restore them.
Make sure you delete the proper file. The delete operation is irreversible.
2. Command line script to delete files the simple way
Deleting files through the command prompt can be done in a variety of ways, with varying levels of confirmation prompts and complexity. One simple way to delete a file through the command prompt is to use the “del” command with the explicit file path. This command will delete the file without any confirmation prompts, so it’s important to double-check the file path before executing the command.
Here is an example of how to use this command to delete a file with the path “C:\Folder\file (1).txt”.
del "C:\Folder\file (1).txt"
Keep in mind that this command will permanently delete the file without moving it to the Recycle Bin, so use it with caution.
3. Delete files with a confirmation prompt
When deleting files with the command prompt, it’s important to exercise caution and double-check the filename before proceeding with the deletion. One way to ensure this is by using the confirmation prompt option. To delete a file with confirmation, you can use the “/p” option followed by the file path. For example, to delete a file named “file (2).txt” located in the “C:\Folder” directory, you can use the command “del /p “C:\Folder\file (2).txt””.
This will prompt a confirmation message asking if you want to delete the specified file. If you confirm the deletion by pressing “Y”, the file will be permanently deleted. Useful for example to delete a file and make sure to double check the filename.
del /p "C:\Folder\file (2).txt"
Then a prompt is displayed:
C:\Folder\file (2).txt, Delete (Y/N)?
4. Use the delete command without confirmation
The other way to delete Windows files with cmd is to disable the confirmation for a large number of files for example. Indeed, if you want to delete multiple files without being prompted for confirmation at all, you can use the delete command in quiet mode.
This mode disables the confirmation prompt and deletes files directly. To use this mode, you can type “del /q” followed by the path and name of the file you want to delete. For example, to delete a file named “file (3).txt” located in the “C:\Folder” directory, you can use the following command.
del /q "C:\Folder\file (3).txt"
5. Delete read only files in command line with force option
When trying to delete a read-only file, the command prompt may display an error message that says:
Access is denied.
This is because the file is set to read-only. To bypass the read-only attribute and force the deletion of the file, you can use the del command with the /f option. For example, to delete a read-only file located in “C:\Folder” named “file (3).txt”, you can use the following command: “del /f C:\Folder\file (3).txt”. This will delete the file without any error messages or prompts.
So, to force deletion of a read only file, and bypass the read-only attribute, use the del /f option, /f stands for force.
del /f "C:\Folder\file (3).txt"
Conclusion on Windows files deletion and its options
To conclude, the “del /p” option is definitely the best cmd option to make sure to delete the proper files from the disk. Use it when only when deleting specific and targeted files. However, to delete a large number of files, use the quiet and the force options.
To delete Windows files using cmd, the command prompt definitely provide several benefits over other methods, i.e. mainly the Windows GUI. It allows for faster deletion of multiple files or directories with a single command, reducing the time needed to perform the task. Additionally, the command prompt can be used to delete files that are otherwise difficult to delete using other methods. The command prompt is a text-based user interface that enables users to execute commands and perform scripted tasks with their computer.
Finally, using the command prompt can be much more efficient for advanced users who are familiar with command line interfaces and prefer to use them over graphical user interfaces. Check out how to copy a file to another folder in command line.