Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / security

Reset file ownership based off parent folder name

0.00/5 (No votes)
12 Jun 2011CPOL 14.1K  
Reset file ownership based off parent folder name
For anyone who does quota management, I'm sure you've run into issues where the Administrator account is incorrectly the owner of 1,000,000 files located in various users' folders. Of course, you can go to each user's folder and manually reset it to the correct owner. But, why?

If your user folders are set up in this format:
D:\Users\<username>


You can simply use FOR loop along with the ICACLS command to reset the ownership of all of the files (among other things).

Here's the snippet:

SQL
@echo off
SET D= D:\users

FOR /F %%A IN ('DIR %D% /AD /B') DO icacls %D%\%%A /setowner %%A /t /c /q


Just put it in a .bat file and you're good to go!

For more information, check out the documentation on ICACLS at
http://technet.microsoft.com/en-us/library/cc753525(WS.10).aspx.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)