Tuesday, April 20, 2010

Unable to delete files in Windows Vista/Windows 7

If you have reinstalled windows machine, without entirely erasing your disk you have probably encountered a problem in which you are unable to delete files or directories. These files belonged to the older windows machine (the previous user(s) of that machine), and therefore, your new operating system will allow you to access these files, but not delete them.

In order to delete these files, we first have to take control over them. This can be done by using 2 command line utilities. First open a command line as Administrator. This can be pressing the “Start” button and writing “cmd” on the search edit box. When the “cmd.exe” file is shown, right click on it and choose: “Run as administrator”. Another way of doing it, is writing “cmd” on the search box, and then pressing “Ctrl + Shift + Enter” instead of Enter.

After the the command line is open as administrator, we first run this command on the file we would like to delete:
takeown /f "file_name" /d y

For example:
takeown /f "c:\some dir\some undeletable file" /d y

If we would like to delete a directory instead of a file, we can use this command, which runs recursively on all the sub directories:
takeown /f "directory_name" /r /d y

For example:
takeown /f "c:\some undeletable directory" /r /d y

After running this command we should run this command on a single file:
icacls "file_name" /grant administrators:F

For example:
icacls "c:\some dir\some undeletable file" /grant administrators:F

And for a directory and it’s sub directories:
icacls "c:\some undeletable directory" /grant administrators:F /t

After running these 2 commands, simply delete the file or directory using the Windows explorer.

No comments:

Post a Comment