2

Looking to find a better way to script this for multiple folders.

Right now I have a icacls script that is reseting permissions on files/folder from a parent folder:

icacls "e:\FTP_Root\user1\*" /q /c /t /reset
icacls "e:\FTP_Root\user2\*" /q /c /t /reset
icacls "e:\FTP_Root\user3\*" /q /c /t /reset

Is there a better way to do this so I don't have to keep adding lines for new users?

Each user folder has its own permissions applied to it so anything inside the users folder gets its permissions reset to inherit from the users folder using the script above.

Jay501
  • 23
  • 1
  • 1
  • 4

1 Answers1

3

You can write a for loop which loops through directory names with the /d switch,

for /d %A in (e:\FTP_Root\user*) do (
    icacls "%A\*" /q /c /t /reset
)

You can always test that the loop is going to do what you want by prepending the icacls line with an echo, which will print the command rather than executing it,

@echo icacls "%A\*" /q /c /t /reset

Here it is on one line, for clarity.

for /d %A in (e:\FTP_Root\user*) do icacls "%A\*" /q /c /t /reset

Here's a wee test, given the file tree below

E:\FTP_ROOT
|
+---user1
|       bar.txt
|       baz.txt
|       foo.txt
|
+---user2
|       bar.txt
|       baz.txt
|       foo.txt
|
\---user3
        bar.txt
        baz.txt
        foo.txt


C:\>for /d %A in (e:\FTP_Root\user*) do icacls "%A\*" /q /c /t /reset

C:\>icacls "e:\FTP_Root\user1\*" /q /c /t /reset
Successfully processed 3 files; Failed processing 0 files

C:\>icacls "e:\FTP_Root\user2\*" /q /c /t /reset
Successfully processed 3 files; Failed processing 0 files

C:\>icacls "e:\FTP_Root\user3\*" /q /c /t /reset
Successfully processed 3 files; Failed processing 0 files

You can always add the @echo after the do to check what the command will actually look like before running it for real.

FOR loops in scripts Windows .bat/.cmd scripts require doubled %% with variables, whereas the shell requires just a single %, so in a cmd shell script the loop would look like this:

    for /d %%A in (e:\FTP_Root\user*) do icacls "%%A\*" /q /c /t /reset
nudl
  • 154
  • 1
  • 7
  • Hello Nudl, I tried your script but I get a "was unexpected at this time. then the next line says for /d \FTP_Root\user* do ( – Jay501 Aug 21 '17 at 14:10
  • Works on my PC ... ;-) I've updated the answer with a demo. – nudl Aug 26 '17 at 09:25
  • Thanks, yes this works if I do it on a command line... if I want to do it as a .bat file thats where it fails. Do I need to do like a cmd /k somewhere if I want it as a bat file? – Jay501 Aug 28 '17 at 15:12
  • See the note on "%%" in scripts. – nudl Aug 29 '17 at 07:19