Set file permissions via batch file

I would like to set permissions for a file in a Batch file, is this possible ?
gisvpnAsked:
Who is Participating?
 
sirbountyConnect With a Mentor Commented:
Greetings gisvpn,
Permissions for what?
If this is an Windows environment, CACLS (or XCACLS) will accomplish this for you.

Displays or modifies access control lists (ACLs) of files

CACLS filename [/T] [/E] [/C] [/G user:perm] [/R user [...]]
               [/P user:perm [...]] [/D user [...]]
   filename      Displays ACLs.
   /T            Changes ACLs of specified files in
                 the current directory and all subdirectories.
   /E            Edit ACL instead of replacing it.
   /C            Continue on access denied errors.
   /G user:perm  Grant specified user access rights.
                 Perm can be: R  Read
                              W  Write
                              C  Change (write)
                              F  Full control
   /R user       Revoke specified user's access rights (only valid with /E).
   /P user:perm  Replace specified user's access rights.
                 Perm can be: N  None
                              R  Read
                              W  Write
                              C  Change (write)
                              F  Full control
   /D user       Deny specified user access.
Wildcards can be used to specify more that one file in a command.
You can specify more than one user in a command.

Abbreviations:
   CI - Container Inherit.
        The ACE will be inherited by directories.
   OI - Object Inherit.
        The ACE will be inherited by files.
   IO - Inherit Only.
        The ACE does not apply to the current file/directory.

~sirbounty
0
 
gisvpnAuthor Commented:
really sorry this is for windows mainly NT, 2000 and XP :)

Thank you i think this will work great. Im not sure if it will do what i want...

Im trying to replace the whole ACL with one person as opposed to editing it, how can i do that ?

0
 
venishjoeCommented:
Hai,

 ~sirbounty gave the syntax.I just show you an example.

item: Delete File
  Pathname=%TEMP%\cacls.bat
end
item: Insert Line into Text File
  Pathname=%TEMP%\cacls.bat
  New Text=echo y| %SYS32%\cacls.exe %UTILDIR% /G Administrators:F /E
  Line Number=0
end
item: Execute Program
  Pathname=%SYS32%\cmd.exe
  Command Line=/c %TEMP%\cacls.bat
  Default Directory=%SYS32%
  Flags=00000010
end
item: Delete File
  Pathname=%TEMP%\cacls.bat
end

ALSO YOU CAN USE LIKE THE CODE GIVEN BELOW

cacls d:\users\aKNOWLEN /E /G "Domain Admins":f /R Everyone
cacls d:\users\hKOBAYASHI /E /G "Domain Admins":f /R Everyone
cacls d:\users\rKOCZERA /E /G "Domain Admins":f /R Everyone
cacls d:\users\aKOENIG /E /G "Domain Admins":f /R Everyone
cacls d:\users\kKOLAKOWSKI /E /G "Domain Admins":f /R Everyone
cacls d:\users\mKOLINSKY /E /G "Domain Admins":f /R Everyone
cacls d:\users\mKOLODZIEJ /E /G "Domain Admins":f /R Everyone
cacls d:\users\mKONCEWICZ /E /G "Domain Admins":f /R Everyone
cacls d:\users\sKONCEWICZ /E /G "Domain Admins":f /R Everyone
cacls d:\users\aKONEFAL /E /G "Domain Admins":f /R Everyone
cacls d:\users\sKONIARZ /E /G "Domain Admins":f /R Everyone
cacls d:\users\dKOPYCINSKI /E /G "Domain Admins":f /R Everyone
cacls d:\users\jKOROMANIAN /E /G "Domain Admins":f /R Everyone
cacls d:\users\jessicaKOROMANIAN /E /G "Domain Admins":f /R Everyone

You can also refer

http://www.tencorp.com/SALESTIP.NSF/0/c14fab6c2db6dce785256bd0004910ae?OpenDocument
http://www.techgalaxy.net/Docs/Dev/Using%20XCACLS%20to%20modify%20NTFS%20permissions.htm

Hope this helps

Regards
Venish
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
sirbountyCommented:
Yep - you can either edit or replace the ACL...good luck!
0
 
gisvpnAuthor Commented:
thank you for everyone comments this works perfect..

There is one thing, when i run this in a batch file it wants me to confirm the change and i have to manually press Y or N... is there anyway to surpress this ?
0
 
venishjoeConnect With a Mentor Commented:
Hai,

The CACLS.EXE command line utility does not provide a /Y switch that automatically answers with Y for Yes to the ARE YOU SURE? Y/N prompt. However, you can use the echo command to pipe the character Y as input to the ARE YOU SURE? Y/N prompt when you call Cacls.exe in a batch file.

To programmatically answer with Yes to the ARE YOU SURE? Y/N prompt during batch file execution, use the following command line in your batch file:

echo y| cacls filename /g username:permission

NOTE: Do not type a space between the "y" and the pipe symbol (|), otherwise, Cacls.exe fails to make the permission changes.

In larger batch files, this may produce the following error before continuing to the next line:

The Process tried to write to a nonexistent pipe.
The echo command does not expel its output before the system has a chance to build the pipe to cacls. There are different ways to work around this.

As an alternative, you can create a dummy file with just a "y" in it and use the syntax:
Cacls "F:\Directory" /g "Domain Users":R < yes.txt

Or you can use the xcacls utility which does allow the /y:
xcacls "F:\Directory" /e /g "Domain Users":R /y

Hope this helps

Regards
Venish
0
 
lukefunoCommented:
so instead of using a gooey interface, you simply use command line to execute same commands?
0
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.

All Courses

From novice to tech pro — start learning today.