Sometimes deleting files or folders do not work on Windows, from the File Explorer. You may experience access denied or locked file message.
Very often, these are video or MP3 files that are recalcitrant to delete or sometimes if the file name is too long.
In this case, you can try to delete the files from the command line.
By deleting a file from the Windows command prompt using CMD commands. We can also go further.
Indeed, this deletion is also accessible by the command prompt from the advanced Windows recovery options when Windows does not start.
Although it is however possible to find a graphical environment from a Live CD.
This tutorial explains how to delete a file or folder from the command line.
CMD Commands to Delete Files
In order to obtain the highest access, it is recommended to launch the command prompt in the admin.
Just do a search on the command prompt then right click and run as an administrator.
The commands we will use in this article are:
- del: delete a file
- rd or rmdir: delete a folder
- takeown: allows to become the owner of files
- icalcs: allows you to redefine file permissions, you must be the owner of the files for this.
The principle is to use the del or rmdir command followed by the file or folder you wish to delete.
You must give the full path of the file you wish to delete.
The user profile is located in C: \ Users \ <yourusername> (replace with your uusername)
- The desktop path is: C: \ Users \ <username> \ Desktop
- The path to the download folder is: C: \ Users \ <username> \ Downloads
You can use the% USERPROFILE% variable to also get the full path that will work on any version of Windows since% USERPROFILE% points to C: \ Users \ <username>, so the desktop path becomes% USERPROFILE% \ Desktop
This is the delete command, it only works to delete files and does not work to delete folders.
The syntax for the del command is as follows:
If no full path is given, del then attempts to delete the file in the current directory.
So if you have this:
So you are in the Windows folder, del will try to delete the file from the Windows folder, so C: \ Windows \ sampledoc.txt
Finally, it is possible to delete the entire contents of a folder using the / S parameter:
del /S C:\Users\username\Desktop\foldername
The rmdir command allows you to delete a folder, it works in the same way as the del command.
So to delete the demo folder from the user’s desktop:
If you want to delete all the sub-folders, you will have to use the / S parameter
rmdir /S C:\Users\username\Desktop\foldername
Resetting Permission Using Command Line
If you do not have permissions and security on the folder, you may encounter an error message during deletion.
It is quite possible to reset file permissions on the command line.
The takeown command allows you to define yourself as the owner of the files.
takeown /F "C:\Program Files (x86)\UCBrowser" /A /R icacls "C:\Program Files (x86)\UCBrowser" /grant:r Users:F /T icacls "C:\Program Files (x86)\UCBrowser" /grant:r administrators:F /T rmdir /S "C:\Program Files (x86)\UCBrowser"
Then we use the icalcs command to modify the permissions, the two commands give the full control over the user and administrator groups (it’s the: F for full which redefines these permissions).
Once the permissions have been modified, we use the rmdir command to delete the folder as explained in the previous paragraph.
You might also like-