Help creating batch file to change permissions of folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
mrees
Posts: 8
Joined: 28 Jun 2014 08:47

Help creating batch file to change permissions of folders

#1 Post by mrees » 28 Jun 2014 09:17

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!!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating batch file to change permissions of folder

#2 Post by foxidrive » 28 Jun 2014 20:46

Which version of Windows is it?

mrees
Posts: 8
Joined: 28 Jun 2014 08:47

Re: Help creating batch file to change permissions of folder

#3 Post by mrees » 28 Jun 2014 20:58

OOPS, I guess that could have been helpful eh?
Apologies there

Windows Server 2003

Thanks :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating batch file to change permissions of folder

#4 Post by foxidrive » 29 Jun 2014 13:40

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

mrees
Posts: 8
Joined: 28 Jun 2014 08:47

Re: Help creating batch file to change permissions of folder

#5 Post by mrees » 01 Jul 2014 06:01

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating batch file to change permissions of folder

#6 Post by foxidrive » 01 Jul 2014 06:26

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.

mrees
Posts: 8
Joined: 28 Jun 2014 08:47

Re: Help creating batch file to change permissions of folder

#7 Post by mrees » 01 Jul 2014 07:43

Thanks the prompting change worked great
adding the user is " didnt work unfortunately

Any other ideas?

:)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating batch file to change permissions of folder

#8 Post by foxidrive » 01 Jul 2014 07:50

Show me the syntax you used in the cacls command to be sure.

mrees
Posts: 8
Joined: 28 Jun 2014 08:47

Re: Help creating batch file to change permissions of folder

#9 Post by mrees » 01 Jul 2014 07:57

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating batch file to change permissions of folder

#10 Post by foxidrive » 01 Jul 2014 07:58

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

mrees
Posts: 8
Joined: 28 Jun 2014 08:47

Re: Help creating batch file to change permissions of folder

#11 Post by mrees » 01 Jul 2014 08:01

No, I get the same error

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating batch file to change permissions of folder

#12 Post by foxidrive » 01 Jul 2014 08:17

What is the error message? It could give us a clue.

mrees
Posts: 8
Joined: 28 Jun 2014 08:47

Re: Help creating batch file to change permissions of folder

#13 Post by mrees » 01 Jul 2014 08:27

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.

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Help creating batch file to change permissions of folder

#14 Post by Compo » 01 Jul 2014 09:26

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

mrees
Posts: 8
Joined: 28 Jun 2014 08:47

Re: Help creating batch file to change permissions of folder

#15 Post by mrees » 02 Jul 2014 04:58

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?

Post Reply