17

I would like to replicate the permissions of a file to another. I do not want to copy the data, only the permissions.

For example, I created a new file and want it to have the same permissions as another file that already existed.

  • Same permissions as another file, but different than the directory they are in? – PsychoData Sep 13 '13 at 21:28
27

You can do it in a few steps with icacls and a text editor.

First you must save the permissions of the original file

icacls C:\test\file1.bin /save perms.txt

you will need to edit the perms.txt file you just created in whatever folder you ran icacls in. The file should look something like this:

file1.bin
D:AI(A;;0x1301bf;;;BU)(A;ID;FA;;;BA)(A;ID;FA;;;SY)(A;ID;0x1200a9;;;BU)(A;ID;0x1301bf;;;AU)

You can ignore all the stuff on the 2nd row, all we care about is the first row. Change the file1.bin to your new filename file2.bin and save the file.

Now you just need to restore the file permissions on the 2nd file, note that we did not include the filename this time. (If you get an error that says "Not all privileges or groups referenced are assigned to the caller." run the program again in an elevated command prompt.)

icacls C:\test\ /restore perms.txt
  • maybe put these into a script to somewhat automate the task? – PsychoData Sep 13 '13 at 21:36
  • 1
    It could be done, but I can't think off of the top of my head how to do step 2 easyly. Anyway if I was going to script it, I would just do the entire thing in Powershell using File.GetAccessControl() and File.SetAccessControl() – Scott Chamberlain Sep 13 '13 at 21:38
4

To make it in a batch (copy security pemissions from a subfolder or all folders of one drive to the same folder/file stricture on another drive), create a, say, cpdacls.bat file with this content:

@echo off
for /r %1 %%f in (.) do call :icacls_one %%f %2

goto :eof

:icacls_one

icacls %1\*.* /save %TEMP%\perms.txt
icacls %2.%~pnx1 /restore %TEMP%\perms.txt
del /q %TEMP%\perms.txt

And then run it like cpdacls.bat e:\ f:\.

Note that the folder structure in the destination folder must be the same as folder structure in the source folder, taken from its root, not depending on what subfolder you have specified as a source folder.

3

The easiest way, by far, is to use PowerShell and run:

Get-Acl .\file1 | Set-Acl .\file2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.