Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have written the following batch file that does a great job combing through all folders beginning with the number 3 and applying folder permissions to any 2010 subfolder. Example of the batch filesis below:

for /D %%f in (D:\Data\3*) do icacls "%%f\2010" /inheritance:r /grant:r "Domain Admins":(OI)(CI)F

Question : How can I improve this script to allow for me to apply the permissions to a specific folder below ANY folder within the folders beginning with 3?

here is an example of my failed attempt:

for /D %%f in (D:\Data\3*) do icacls "%%f*\specificfolder" /inheritance:r /grant:r "Domain Admins":(OI)(CI)F

share|improve this question
note on my last line of code, the post is stripping a \ prior to the * –  user153521 Aug 21 '12 at 0:39
Can you provide an example directory tree that you are working on? –  soandos Aug 21 '12 at 0:42
@soandos Example Directory: D:\Data\3214 Client\Accounts\SpecificFolder The idea being it will look for the SpecificFolder within any Folder below the 3214 'Client Directory.' –  user153521 Aug 22 '12 at 17:16
add comment

1 Answer

With the following directory tree:



31245 Client



31246 Client



The code below will apply the security permissions you specified to both of the directories called "specificfolder"

@echo off
for /D %%f in ("D:\Data\3*") do for /D %%d in ("%%f\*") do icacls "%%d\specificfolder" /inheritance:r /grant:r "Domain Admins":(OI)(CI)F

NOTE - This uses nested FOR loops which is not supported by DOS (COMMAND.COM). This will work fine with a NT (CMD.EXE) system.

share|improve this answer
Thanks for your reply on this. The command in your answer is what I am currently using for everything within D:\Data\3* directories just as you stated, and it works great. I had placed the asterik in "%%f*\specificfolder" because I would like the script to drill down one more directory (with any name, which is the reason for the *) and look for the specific folder there. I hope this helps, let me know if I need to explain it in more detail. –  user153521 Aug 22 '12 at 17:12
Updated my original answer to reflect the directory structure you described above. If you want/need something more, let me know. –  Slayer537 Aug 22 '12 at 22:48
We are close! I tried the code but it is cutting off the directory between client number and name due to the space. I have modified your most recent code as follows, I just added quotes around "%%f*": for /D %%f in (D:\Data\3*) do for /D %%d in ("%%f*") do icacls "%%d\specificfolder" Example: D:\Data\31245 Smith\anyfolder\specificfolder The problem is after the script runs through all the correct folders, it then repeats itself but it cuts off the Client directory prior to the space like the most recent code. Example: icacls D:\Data\31245\anyfolder\specificfolder –  user153521 Aug 23 '12 at 22:58
I'm unable to replicate your problem. What bothers me is that you say the script repeats itself. Are you using this code in combination with some other code? I edited my answer again and also added quote around the D:\Data\3* portion; however, on my system it worked just fine with only making the change you mentioned. Perhaps you are referring to it echoing? Try making a new batch file with the two lines of code in my answer, and let me know what happens. If it doesn't work, give me the full path you are using. –  Slayer537 Aug 24 '12 at 2:29
My fault, the script did not repeat, I misinterpreted this yesterday with two similar named files in the test environment. –  user153521 Aug 24 '12 at 21:20
show 2 more comments

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.