Solved

Set file permissions via batch file

Posted on 2004-04-21
7
12,693 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 67

Expert Comment

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

Author Comment

by:gisvpn
Comment Utility
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
Comment Utility
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
Comment Utility
so instead of using a gooey interface, you simply use command line to execute same commands?
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
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.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now