recursively change owner windows 7

Somehow I accidentally set all the files in a subfolder to "No Owner' and I can't seem to change all the permissions. I can change one by hand by changing the owner then setting permissions but how can I change owner of all the files in this directory at once?


Solution 1:

Use takeown from the command prompt to take ownership a folder, all its subfolders and files recursively:

takeown /f "c:\folder\subfolder" /r

This works well, but if you don't run your command line console as administrator it may fail for files you don't own.

Solution 2:

To fix really broken permissions, the best is to run these two commands one after the other:

takeown /f "C:\path\to\folder" /r
icacls "C:\path\to\folder" /reset /T

The first one will give you ownership of all the files, however that might not be enough, for example if all the files have the read/write/exec permissions set to "deny". You own the files but still cannot do anything with them.

In that case, run the second command, which will fix the broken permissions.

Solution 3:

Note that cacls is deprecated (since Windows Vista?) and it advises you to use icacls.

This command will recursively reset the permissions on a folder:

icacls "C:\path\to\folder" /reset /T

Solution 4:

You can use cacls from the command prompt:

cacls "C:\path\to\folder" /E /T /C /G "Administrator":F

The /T switch allows it to function recursively. Replace Administrator with the user you wish to give permissions to.

Solution 5:

I had problems with files with very long paths (greater than 256 characters). The two commands

takeown /f "C:\path\to\really_long_folder_name" /r
icacls "C:\path\to\really_long_folder_name" /reset /T

worked except for these files with really long paths and names. I ended up renaming

"C:\path\to\really_long_folder_name"

to

"C:\path\to\r"

and then running

takeown /f "C:\path\to\r" /r /D Y
icacls "C:\path\to\r" /grant Everyone:(F) /t /c /q

after which I could rename the folders to something more sensible. takeown's /D Y answers yes to prompts. For icacls I used /grant to give full access to everyone (as I was just trying to access data on a hard drive from a dead PC), with /t to process sub folders and files, /q to run in quiet mode (hide successes) and /c to show errors. I repeated the process, renaming folders until all the files permissions were updated successfully.

Hopefully this helps someone who has come across errors similar to the "failed to enumerate objects in the container access is denied" errors I was getting when trying to gain access to the data from an old hard drive.