Batch file to create a folder in multiple other folders and assign NTFS Perms to it

I have a series of folders which receive .pdf files from paper scans made by staff at a photocopier. One folder per Business Unit on a Windows 2000 server, so:

Scan$ Unit 1\ Unit 2\ Unit 3\ 

What I'm trying to do is write a batch file to create a folder named:

'Temp area - files will be deleted after 14 days !'

and place it into each of the Unit 1,2,3 'root' folders to remind staff not to store data there long-term. Once I have created that folder I need to set the NTFS perms, maybe with icacls, to only allow Domain Admins to access the folder I created, so staff cannot delete it.

I've written batch files before to create 'home' folders reading names from a .txt, eg for /f %%a in (users.txt) do ..icacls \\filer\Personal$\Home\%%a /grant:r "Company\Domain Admins:(OI)(CI)F"

I'm struggling with this issue, any help appreciated :-)

2

1 Answer

You should be able to accomplish this with this simple script:

@ECHO off
SET _Path=\\unc\scan$
SET _FolderName=Temp area - files will be deleted after 14 days!
for /f "delims=" %%a in ('dir "%_Path%" /B /R /AD') DO ( mkdir "%_Path%\%%a\%_FolderName%" icacls "%_Path%\%%a\%_FolderName%" <...>
)

Breakdown

  • SET _Path=\\unc\scan$ sets a variable holding the path to the folder containing the Unit 1, Unit 2, Unit 3 folders. Setting this in only one places makes it easier to change in the future
  • SET _FolderName=Temp area - files will be deleted after 14 days! sets a variable holding the name of the folder you want to create and lock
  • dir "%_Path%" /B /AD lists the subdirectories in the path you specified
  • for /f "delims=" %%a in ('dir "%_Path%" /B /R /AD') DO will loop through the subdirectories
  • mkdir "%_Path%\%%a\%_FolderName%" creates your new subdirectories using the folder name specified
  • icacls "%_Path%\%%a\%_FolderName%" <...> will run your icacls to set the permissions. You'll have to replace <...> with whatever /grant or /deny permissions you want to set.

References

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like