Use Powershell, not a bat file.
Build a csv to identify the owners.
"DeptCode","DeptHead"
"00","Larry"
"01","Moe"
"02","Curley"
I tested with this structure.
md C:\temp\Tdrive\Legal01ABCDEFG\Contracts\Signed\
md C:\temp\Tdrive\Legal01HIJKLMN\Contracts\Signed\
md C:\temp\Tdrive\Legal02ABCDEFG\Contracts\Signed\
md C:\temp\Tdrive\Legal03ABCDEFG\Contracts\Signed\
md C:\temp\Tdrive\Legal01ABCDEFG\Contracts\Other\
md C:\temp\Tdrive\Legal01HIJKLMN\Contracts\Other\
md C:\temp\Tdrive\Legal02ABCDEFG\Contracts\Other\
md C:\temp\Tdrive\Legal03ABCDEFG\Contracts\Other\
This script processes the folders+csv and just displays the icacls command. Remove the outer quotes to actually run it.
Test test test before you update 15k folders. Make sure that the icacls format is correct.
$Csv = Import-Csv "C:\temp\test.csv"
$BaseFolder = "C:\temp\Tdrive\"
$TopFolders = Get-ChildItem -Path $BaseFolder -Directory
foreach ($TF in $TopFolders) {
""
"Processing folder {0}" -f $TF.name
$Dept = $TF.name.Substring(5,2) # Assuming the word 'Legal' followed by a 2 digit code
$Dept
# https://devblogs.microsoft.com/scripting/powertip-get-row-from-csv-file-based-on-value
$DeptHead = $csv.Where({$_.DeptCode -eq $Dept}).DeptHead
if ($DeptHead) {
$DeptHead
"icacls.exe $($tf.fullname) /grant `"$($DeptHead):(CI)(RX)`""
$SignedFolders = Get-ChildItem -Path $tf.FullName -Filter "Signed" -Directory -recurse
foreach ($sf in $SignedFolders) {
"icacls.exe $($sf.fullname) /grant `"$($DeptHead):(OI)(CI)(RX)`""
}
} else {
"Error! Department not found in CSV."
}
}
Note: as a retired server admin, I recommend using groups and not individual user accounts. That way when you eventually get the request for "Larry has resigned, and Moe has been named department head, grant Moe whatever access that Larry had", you just add Moe to the department owner group and remove Larry. And you don't have to go searching the file system for permissions that reference Larry.
I'm not really good at scripting
Download and refer to this documentation.