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

Hello,

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!
Skarley GuerreroAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
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?

Steve
0
Lionel MMSmall Business IT ConsultantCommented:
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
0
Steve KnightIT ConsultancyCommented:
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.

Steve
0
Steve KnightIT ConsultancyCommented:
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?

Steve

@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
SETLOCAL ENABLEDELAYEDEXPANSION
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve KnightIT ConsultancyCommented:
I wold suggest as follows:

Accept dragon-it http:#a42377542
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.