How to create Subfolders and assign permissions

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
bimbo
Posts: 3
Joined: Sun Nov 25, 2007 5:18 am

How to create Subfolders and assign permissions

Post by bimbo » Sun Nov 25, 2007 6:13 am





I am new to scripting and I am trying to write a script that would create a folder structure and subfolders similar to pic below:

MainFolder |_ Subfolder1 |_ Subfolder 1A |_ Subfolder 1B |_ Subfolder2 |_ Subfolder 2A |_ Subfolder 2B |_ Subfolder 2C
|_ Subfolder3 |_ Subfolder 3A |_ Subfolder 3B |_ Subfolder 3B1 |_ Subfolder 3B2
The script would perform the following functions:
STEP 1) Prompt the user to enter the name of the "MainFolder" to create. (e.g User1) If "MainFolder" already exist then exit. Else creates folder and subfolders.
STEP 2) Assign permissions to a folder and subfolders for specific users groups. (Administrator Full Control, Sales (read), HR (Modify)
I was able to find a script that performs most of the task in Step1.
a) Prompt user to enter the name of the folder to create b) Create folder and Subfolders
The script does not allow me to create folders within a Subfolder as per below pic |_ Subfolder3 |_ Subfolder 3A |_ Subfolder 3B |_ Subfolder 3B1 |_ Subfolder 3B2 The other issue, I'm not sure how to incorporate STEP 2 in the script as well.

Thank very much for your help
**** Script Sample ***
strRoot="c:Test" Set objFSO=CreateObject("Scripting.FileSystemObject")Set objFolder=objFSO.GetFolder(strRoot)Set colFolders=objFolder.SubFolders strFolder = InputBox("Please Enter a MainFolderName.") If objFSO.FolderExists(strRoot & strFolder) Then WScript.Echo "Folder " & strRoot & strFolder &_ " already exists"Else colFolders.Add strFolderEnd If Set objFolder=objFSO.GetFolder(strRoot & strFolder)Set colFolders=objFolder.SubFoldersarrFolders=Array("Subfolder1","Subfolder2","Subfolder3") For Each item In arrFolders If objFSO.FolderExists(strRoot & strFolder & "" & item) Then WScript.Echo "Folder " & strRoot & strFolder &_ "" & item & " already exists" Else colFolders.Add item End IfNext


User avatar
bimbo
Posts: 3
Joined: Sun Nov 25, 2007 5:18 am

How to create Subfolders and assign permissions

Post by bimbo » Sun Nov 25, 2007 6:13 am





I am new to scripting and I am trying to write a script that would create a folder structure and subfolders similar to pic below:

MainFolder |_ Subfolder1 |_ Subfolder 1A |_ Subfolder 1B |_ Subfolder2 |_ Subfolder 2A |_ Subfolder 2B |_ Subfolder 2C
|_ Subfolder3 |_ Subfolder 3A |_ Subfolder 3B |_ Subfolder 3B1 |_ Subfolder 3B2
The script would perform the following functions:
STEP 1) Prompt the user to enter the name of the "MainFolder" to create. (e.g User1) If "MainFolder" already exist then exit. Else creates folder and subfolders.
STEP 2) Assign permissions to a folder and subfolders for specific users groups. (Administrator Full Control, Sales (read), HR (Modify)
I was able to find a script that performs most of the task in Step1.
a) Prompt user to enter the name of the folder to create b) Create folder and Subfolders
The script does not allow me to create folders within a Subfolder as per below pic |_ Subfolder3 |_ Subfolder 3A |_ Subfolder 3B |_ Subfolder 3B1 |_ Subfolder 3B2 The other issue, I'm not sure how to incorporate STEP 2 in the script as well.

Thank very much for your help
**** Script Sample ***
strRoot="c:Test" Set objFSO=CreateObject("Scripting.FileSystemObject")Set objFolder=objFSO.GetFolder(strRoot)Set colFolders=objFolder.SubFolders strFolder = InputBox("Please Enter a MainFolderName.") If objFSO.FolderExists(strRoot & strFolder) Then WScript.Echo "Folder " & strRoot & strFolder &_ " already exists"Else colFolders.Add strFolderEnd If Set objFolder=objFSO.GetFolder(strRoot & strFolder)Set colFolders=objFolder.SubFoldersarrFolders=Array("Subfolder1","Subfolder2","Subfolder3") For Each item In arrFolders If objFSO.FolderExists(strRoot & strFolder & "" & item) Then WScript.Echo "Folder " & strRoot & strFolder &_ "" & item & " already exists" Else colFolders.Add item End IfNext


User avatar
jhicks
Posts: 1789
Joined: Sun Jan 21, 2007 11:31 pm

How to create Subfolders and assign permissions

Post by jhicks » Sun Nov 25, 2007 8:22 pm

Truly, the best approach for setting permissions is to use a command line tool like CACLS or XCACLS. You can call it from your VBScript using the RUN or EXEC methods. I know you've invested some time in a VBScript approach, but you could quickly get this all done in a batch file. When you specify a path with the MKDIR command, it creates all the folders in the path:C: mkdir mainfoldersubfolder1subfolderBfoobarAll you have to do is specify the complete tree for each branch. Then call CACLS

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

How to create Subfolders and assign permissions

Post by jvierra » Tue Nov 27, 2007 5:35 am

There was a script solution in teh ScriptVault but it seems to have disappeared. I recreated it and uploaded.

Here is a text copy of the script.

uploads/2491/MkDir.txt

Somrtimes we don''t want to shell out of script and need to make a folder path. This code behaves similar to MD/MkDir. and is easy to use and reliable/dast under most circumstance.

Note that teh fso.DeleteFOlder method WILL delete all subfolders and files. It is only the fso.CeateFolder method that is not recursive - don't understabd why but MS was asleep the day they built that one.
jvierra2007-11-27 12:56:07

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

How to create Subfolders and assign permissions

Post by jvierra » Sun Dec 02, 2007 3:53 pm

You could mutiple input boxes but I would use teh "ReadLine" method of WScript,StdIb.ReadLine method.

You could also design an HTA to gather multiple lines of input.

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

How to create Subfolders and assign permissions

Post by jvierra » Fri Dec 07, 2007 2:25 am

I think this is closer to what you want.

Code: Select all

	
RunCACLS "d:testp", "/E /G testme:F"
Function RunCACLS( sFile, sPerms )
	
    Set objShell = CreateObject("Wscript.Shell")
   
    intRunError = objShell.Run("cacls " & sFile & " " & sPerms , 2, True )  
        
    If intRunError  0 Then
        If intRunError = 2 Then
                Wscript.Echo "File/Folder not found:" & strHomeFolder
        Else
            Wscript.Echo "CACLS Error:" & intRunError & ":" & sFile & ":" & sPerms
            SetPerms = False
        End IF
    End If
    
    SetPerms = True
    
End Function

User avatar
bimbo
Posts: 3
Joined: Sun Nov 25, 2007 5:18 am

How to create Subfolders and assign permissions

Post by bimbo » Sat Dec 08, 2007 3:41 am


The issue with not being able to assign NTFS permissions via cacls was due to a syntax error on my side due to a added blank space between Everyone and F.

perms = Array ( _ "Sales | /T /P Everyone: F", _ "SalesPending | /t /c /g Everyone: F < yes.txt" _

Once I removed the blank space the program started working correctly.


perms = Array ( _ "Sales | /T /P Everyone:F < yes.txt", _ "SalesPending | /t /c /g Everyone:F < yes.txt "_

Againg thank you for you help.

Locked