We help IT Professionals succeed at work.

Is there a way to set user permissions for a folder n directories deep without knowing the name of the folder?

Skarley Guerrero

I am writing a batch file to set user permissions for a plethora of folders. For one specific folder, I would like to set different permissions for each directory and sub-directory. The issue I'm having is getting to a certain level and setting permissions for all those folders. Example:

Main folder > (20 Folders in Main Folder) > (50 Folders in each sub-Main Folder) > (5 folders after that) > pdf files

The numbers are arbitrary, but that's somewhat how the construction of our file system looks like.

the directory right before the pdf files (5 folders after that) should have modify permissions. each sub-folder before that should have only folder creation permissions. Finally, the Main folder has read-only permissions.

What I have attempted so far is this batch script:

icacls "Main Folder" /grant:r %username%:M /T    

FOR /D %%S in ("Main Folder\*") do icacls "%%S" /grant:r Users:(R, AD) /grant:r %username%:(R, AD)

icacls "Main Folder" /grant:r Users:R /grant:r %username%:R

This sets the permissions I want but only up to this point:  Main folder > (20 Folders in Main Folder) > (50 Folders in each sub-Main Folder)

I've tried nesting a FOR loop to get into that last directory, but had no success.

Thank you!
Watch Question

So your five levels means 5 more subdirectories deep or 5 more at the same level?

Can you get a list of the right ones using

Dir main folder\sub folder\*\ /ad /b or with 5 x \* if it is 5 folders deeper?

Lionel MMSmall Business IT Consultant

if I understand you right, you have a folder named test1 and it is 60 folder-sub folder deep? and you don't know its name? if that is what you are asking then the only way to assign folder test1 specific permissions it to assign is to assign the required permissions to an earlier "parent" folder and turn on inheritance. So you will assign a folder above it the required permissions and then enable inheritance and assign that same permissions to that folder and all its sub-folders for example to give Full Control to  Domain-name\user-name to folder d:\parent-folder-name and all its subfolder and files. You can also put this in batch file and loop through a list of known folder names.
icacls d:\parent-folder-name /inheritance:r /grant:r Domain-name\user-name:(OI)(CI)F /T

Above dir won't work of course.  Suggest you post few example directories but how about this to get the raw directory names?

dir /ad /b /s | findstr /i /r "x:\\Top Level\\Next level\\.*\\.*\\.*\\.*\\*"

etc. i.e to match

x:\top level\next level followed by \ then any characters, another backslash etc....

You can soon then use that in a FOR or FORFILES command to run icacls etc.

OK one more try!  This will run down under the My Pictures directory then split the directory listing at each \ using FOR command.  It checks that the 8th token is NOT blank, then if the ninth token IS blank then it echos the full path (so that it doesn't include subdirs under that - assume you would put inherited permissions on that level).

i.e. that would match this, i.e. 5 extra levels down and any others at that depth

D:\1. Data\My Pictures\level3\level4\level5\level6\level7

Please give us example paths if needed?


@echo off
set startdir="D:\1. Data\My Pictures"
cd /d "%startdir%"
for /f "delims=\ tokens=1-8*" %%A in ('dir /ad /b /s') do (
  If not "%%~H"=="" (
	if "%%~I"=="" echo %%A\%%B\%%C\%%D\%%E\%%F\%%G\%%H

Open in new window

Other example with multiple level checks:

@echo off
set startdir="d:\mystart\dir"
cd /d "%startdir%"
for /f "delims=\ tokens=1-9*" %%A in ('dir /ad /b /s') do (
  If not "%%~E"=="" (
    if "%%~F"=="" (
      Set DirName=%%A\%%B\%%C\%%D\%%E
      echo Found a forth level directory on !dirname! 
  IF NOT "%%~D"=="" (
    if "%%~E"=="" (
      ECHO At a third level in %%A\%%B\%%C\%%D

Open in new window

I wold suggest as follows:

Accept dragon-it http:#a42377542