Solved

Set file permissions via batch file

Posted on 2004-04-21
7
12,707 Views
Last Modified: 2008-07-01
I would like to set permissions for a file in a Batch file, is this possible ?
0
Comment
Question by:gisvpn
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 250 total points
ID: 10876804
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
 

Author Comment

by:gisvpn
ID: 10876858
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
 
LVL 4

Expert Comment

by:venishjoe
ID: 10876920
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 67

Expert Comment

by:sirbounty
ID: 10876987
Yep - you can either edit or replace the ACL...good luck!
0
 

Author Comment

by:gisvpn
ID: 10877613
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
 
LVL 4

Assisted Solution

by:venishjoe
venishjoe earned 250 total points
ID: 10879838
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
 
LVL 3

Expert Comment

by:lukefuno
ID: 21481711
so instead of using a gooey interface, you simply use command line to execute same commands?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question