Using icacls in a batch file

I want to create a batch file that applies permissions for a given user folder using icacls. This is the batch file I made:

  @echo off

 set / p username = Enter username:

 echo Select permissions:

 echo N - no access
 echo F - full access
 echo M - modify access
 echo RX - read and exe
 echo R - read-only acc
 echo W - write-only ac
 echo D - delete access

 echo.

 set / p perm = Enter permissions:

 if% perm% == F

 icacls "C: \ Users \% username%" / grant: r "% username% :( OI) (CI) F" 

When I run this file and enter permissions as F, it displays this error : the command syntax is incorrect. but if I run the same command directly in cmd, it works fine. So, how do I fix a command in a batch file so that it runs without any problems?

+4
source share
1 answer

It seems that the syntax of your if command is causing problems. You check if permission F selected, but then nothing is done with it.

Try either putting it on one line

 if %perm%==F icacls "C:\Users\%username%" /grant:r "%username%:(OI)(CI)F" 

or in brackets

 if %perm%==F ( icacls "C:\Users\%username%" /grant:r "%username%:(OI)(CI)F" ) 
+3
source

All Articles