Icacls batch file multiple directories with wildcards help needed

icaclswildcards

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

Best Answer

With the following directory tree:

D

Data

31245 Client

anyfolder

specificfolder

31246 Client

anyfolder

specificfolder

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.

Related Question