Page 1 of 2

Help creating batch file to change permissions of folders

Posted: 28 Jun 2014 09:17
by mrees
Hi guys & girls

I'm very new to batch file creation and would love some help

I need to be able to change the NTFS permissions on a particular folder using a script (so i can schedule on a daily basis)
I have hundreds of folders under a file MP3 share (eg: \\server\music\black sabbath\(1970) Paranoid\*.mp3)
In that same folder, I have a folder called FLAC (eg: \\server\music\black sabbath\(1970) Paranoid\FLAC\*.flac)
I need the FLAC folders (including their contents) to have their current permissions removed and then their permissions set to rwx for administrator and a 2nd user

I don't want to effect the permissions of the MP3 files, just the FLAC's

Any help would be awesome

Thankyou in advance!!

Re: Help creating batch file to change permissions of folder

Posted: 28 Jun 2014 20:46
by foxidrive
Which version of Windows is it?

Re: Help creating batch file to change permissions of folder

Posted: 28 Jun 2014 20:58
by mrees
OOPS, I guess that could have been helpful eh?
Apologies there

Windows Server 2003

Thanks :)

Re: Help creating batch file to change permissions of folder

Posted: 29 Jun 2014 13:40
by foxidrive
This is untested: try it on some dummy folders and files first.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir "d:\flac" /b /s /ad ') do (
   echo y| cacls "%%a" /C /G administrator:F /G user2:F
   echo y| cacls "%%a\*.*" /C /G administrator:F /G user2:F
)
pause

Re: Help creating batch file to change permissions of folder

Posted: 01 Jul 2014 06:01
by mrees
Thanks so much foxidrive

Currently its prompting for each change, needing to say Y or N
Also, how can I add the 2nd user when the user has a space in it?

Thanks a lot for your help, REALLY appreciate it

Re: Help creating batch file to change permissions of folder

Posted: 01 Jul 2014 06:26
by foxidrive
See my edited code above - I added echo y| to each line to answer the prompt with y

Put the username in "double quotes" and see how that goes.

Re: Help creating batch file to change permissions of folder

Posted: 01 Jul 2014 07:43
by mrees
Thanks the prompting change worked great
adding the user is " didnt work unfortunately

Any other ideas?

:)

Re: Help creating batch file to change permissions of folder

Posted: 01 Jul 2014 07:50
by foxidrive
Show me the syntax you used in the cacls command to be sure.

Re: Help creating batch file to change permissions of folder

Posted: 01 Jul 2014 07:57
by mrees
Thankyou!

@echo off
for /f "delims=" %%a in ('dir "C:\Users\Administrator\Desktop\Tunez\flac" /b /s /ad ') do (
echo y| cacls "%%a" /C /G administrator:F /G "John Smith":F
echo y| cacls "%%a\*.*" /C /G administrator:F /G "John Smith":F
)
pause

Re: Help creating batch file to change permissions of folder

Posted: 01 Jul 2014 07:58
by foxidrive
Does this work?

Code: Select all

@echo off
for /f "delims=" %%a in ('dir "C:\Users\Administrator\Desktop\Tunez\flac" /b /s /ad ') do (
echo y| cacls "%%a" /C /G administrator:F /G "John Smith:F"
echo y| cacls "%%a\*.*" /C /G administrator:F /G "John Smith:F"
)
pause

Re: Help creating batch file to change permissions of folder

Posted: 01 Jul 2014 08:01
by mrees
No, I get the same error

Re: Help creating batch file to change permissions of folder

Posted: 01 Jul 2014 08:17
by foxidrive
What is the error message? It could give us a clue.

Re: Help creating batch file to change permissions of folder

Posted: 01 Jul 2014 08:27
by mrees
It just shows switching information
However if that user info is removed, the batch works as expected.
Its a bit strange

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.
ID - Inherited.
The ACE was inherited from the parent directory's ACL.

NOTE: Cacls is now deprecated, please use Icacls.

Displays or modifies access control lists (ACLs) of files

CACLS filename [/T] [/M] [/L] [/S[:SDDL]] [/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.
/L Work on the Symbolic Link itself versus the target
/M Changes ACLs of volumes mounted to a directory
/S Displays the SDDL string for the DACL.
/S:SDDL Replaces the ACLs with those specified in the SDDL string
(not valid with /E, /G, /R, /P, or /D).
/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 than one file in a command.
You can specify more than one user in a command.

Re: Help creating batch file to change permissions of folder

Posted: 01 Jul 2014 09:26
by Compo
Have you tried it without using the grant switch multiple times?

Code: Select all

@Echo Off
For /f "Delims=" %%A In (
   'Dir/b/s/ad "C:\Users\Administrator\Desktop\Tunez\flac"') Do (
   Echo y|Cacls "%%A" /c /g Administrator:f "John Smith":f
   Echo y|Cacls "%%A\*.*" /c /g Administrator:f "John Smith":f)
Pause

Re: Help creating batch file to change permissions of folder

Posted: 02 Jul 2014 04:58
by mrees
Thankyou so very much!
That WORKED!

I really appreciate all your help Compo & foxidrive

What command can I also add to make the FLAC folders hidden and RO?