I have an external hard drive with some software installation files. The hard drive was formatted as NTFS. After a few years of use, it had a lot of temporary directories used by installers. When I tried to delete those directories, I got this dialog:
If I clicked on Try Again, the same dialog popped up. The only way to get out of it was to click on Cancel. Unfortunately, the directory still remained and nothing got deleted. I later found out that the problem was due to the fact that the folders were owned by different user IDs when I used it on a different computer. In order for me to delete the folder (along with the files and folders stored within it), I had to take ownership of all the files and folders in the folder and set permissions for them. Finally, I could delete the folder. The steps using Windows Explorer were not straightforward and proved to be slow. I tried various combinations of commands and wrote a script to help me delete the folders.
Here is the script.
takeown /r /d y /f %1
cacls %1 /t /e /g everyone:f
del %1\*.* /s /q
rmdir /s /q %1
Create a text file called deldir.bat and copy the above commands to the file and save it. Copy the script to a directory in your PATH variable. If you do not know the PATH variable value, copy the script to c:\Windows\system32. Before you use the script, make sure you really want to delete the folder. The script will not prompt you for confirmation; it will just delete the folder you specified. To use it, first open up an elevated command prompt. Once you are in the elevated command prompt, type in the following to delete the folder:
For example, if I wanted to delete the folder at G:\temp_folder, I would type the following:
You may see some warnings, but the directory should be deleted. In case you were wondering what the script does, I can help explain it for you. The first command takeown lets you take ownership of the specified folder and files and folders under it. The cacls command grants full permission to all users of the files and folders in question. The del command deletes all the files under the folder. Finally, the rmdir command removes the folder.
Please let us know if this script works for you or not.
Note that this script deletes the folders and files that you do not own. If a file cannot be deleted because some process are locking the file, you’ll need a program called unlocker to unlock the file first. You can google unlocker to find the program. When you install the program, make sure you pick the advanced option and do not install the delta search program that comes with unlocker’s installer.