Scripting School: In-depth look at a script for creating folders

Last month's scripting column gave an overview of what's involved in programmatically creating new folders and setting permissions on them. This month, Christa Anderson examines the script that performs these tasks.

Editor's note: This is the 14th column in a continuing series on scripting that appears monthly on SearchWinSystems.com. Feel free to send any scripting questions to the author, Christa Anderson, at editor@searchwincomputing.com.

By submitting your personal information, you agree to receive emails regarding relevant products and special offers from TechTarget and its partners. You also agree that your personal information may be transferred and processed in the United States, and that you have read and agree to the Terms of Use and the Privacy Policy.

Last month's column gave an overview of what's involved in programmatically creating new folders and setting permissions on them. This month, we'll examine the script that performs these tasks.

Creating new folders from a list of names One way to create this set of new user folders would be to plug the names into the script, but that wouldn't be very convenient for more than about five users and would make your script hard to update to add more users. Therefore, it's simpler to do any kind of large-scale maintenance using a list. A simple text file would work, but let's create our list in Excel. Doing so allows us to take advantage of Excel's own programming capabilities.

What programming capabilities, you ask? You probably know that you can type formulas into Excel to make calculations. Well, you can also use formulas (formulas similar to VBScript's functions) to calculate names.

For instance, let's say that at our company, Acme Corp., everyone's user name consists of the first three letters of their first name and the last five letters of their last name. So, you can create an Excel spreadsheet with headers that look like this:

User Name

First Name

Last Name

First Name and Last Name are explicitly what they sound like: the user's first name and last name. User Name, however, is a function of the other two: =LEFT(B2,3)&LEFT(C2,5). This function reads the first three letters of cell B2 and the first 5 of C2 and concatenates them (See the ampersand? We've used it to concatenate strings before) into a single username. My full name is Christa Anderson, so my username becomes ChrAnder, which we can employ to name our user home directories. Conveniently, the LEFT function in Excel works almost exactly like the LEFT function in VBScript, so it's easy to remember. Drag the formula to populate all the cells in the User Name column.

How will you use this Excel workbook to create the folders? The File System object comes into heavy play during this operation. Not only will we use it to create the home directory folders, but we'll also use it to open the Excel file, like this:

What we're doing is creating an object to represent an FSO, then using that FSO to open Excel. Open the workbook in which you stored the user names, then use the Excel object to refer to the cells within it. I am incrementing rows each time by updating the iRow variable to equal itself plus 1. The Do Until…Loop conditional statement here will continue reading the workbook until the first cell, where we stored the user name, is empty.

(Do While is similar in function to Do Until, but harder to use in this script. Do While would work only if we were testing for the first cell in the row not being empty. It would work, but it's more complicated to format the function.)

Shut down Excel when you're done using it by using the Quit method on the Excel object—you'll save memory by closing applications you don't need.

Now that we can read the Excel object, it's time to create the folders. We're going to haul out the File System object again to create the folders and name them by concatenating the user name with the root path for the home directories. Then we'll use the Create Folder method to create the folders. For maximum flexibility in your script, make the path to the home directory root a variable, so you can update it if required. In this case, I'm using a local path, but sHome can also be a UNC share and your script will be more flexible if it is.

This works – but only once. Do it a second time, and the script will fail with an error because those folders already exist. Well, you may be thinking, that's fine -- I don't need to run the script twice, right? Not really. You do need to run it again -- unless your company will never, ever hire anyone ever again.

Rather than write a new Excel file each time you make hires, you'll just keep adding to the one you've got. Therefore, you will run this script twice and will need error correcting to help you avoid trying to create folders that already exist. Try amending the code to look like the following, which will both check for existing folders and tell you that they're already made. (You may want to skip the notification -- getting a list of five hundred user folders each time you run the script will eventually get dull.)

If oFSO.FolderExists(sHomeDir) Then
Set oFolder = oFSO.GetFolder(sHomeDir)
WScript.Echo sHomeDir&"'s home directory already exists."
Else
oFSO.CreateFolder(sHomeDir)
End If

Setting folder permissions As we discussed last month, the File System object doesn't have any properties that set user permissions. Therefore, we'll set permissions using CACLS. I don't want to go with the default permissions, as they don't keep other users out of each other's home directories. Take a look at this CACLS output from a newly created user folder:

Deny the specified user access to the named path. This is not the same thing as no permissions.

Our goal for these new folders is to give users and administrators full rights over their own folders, replacing any rights that may have already been in place. These rights need to apply to all folders that might be within that subfolder. (While it may appear that the simplest way to solve this problem would be to apply permissions at the root, that doesn't work because we want to differentiate access to individual user folders based on identity.) If we're using my account, the command using CACLS directly looks like this:

cacls c:scriptshomechrander /t /c /g administrators:f chrander:f

Test it first by running the command from the command line. When you've done so and the permissions have changed so that the user and administrators have full permissions and no one has any other permissions, you're ready to replace the username with the variable. We're using WshShell's Run method below to execute CACLS.

The Run method above uses the environment variable COMSPEC to specify the command line, not cmd.exe. The Echo Y sends a Y to the screen, since CACLS asks if you're sure before continuing. The rest of the command should be pretty obvious once you play with CACLS for a bit -- just be sure about your quotation marks. Finally, in the script above, the "2" says that the command window should begin in a minimized state and the "True" says that the script should wait until this command has finished executing before it continues.

Summary This month's script, shown in its entirety as "makehomedir.txt" below, shows you how to perform a simple, but common administrative task: setting up new users with home directories. (You can copy the script with a vbs extension and modify the variables as appropriate to run it). Here is the full script:

You've seen how to run external commands, access and incorporate into your scripts data stored in an Office application, and how to make your scripts capable of avoiding a common error. Keep that Excel spreadsheet handy -- it will be useful later!

Christa Anderson A Terminal Services MVP, Christa Anderson is the strategic technology manager for visionapp. A former program manager for the Microsoft Terminal Services team, she is an internationally known authority on server-based computing. She is the author of Windows Terminal Services, The Definitive Guide to MetaFrame XP, and co-author of the book Mastering Windows 2003 Server. If you have a scripting question for Christa, please e-mail her at editor@SearchWincomputing.com. She often uses these emails as fodder for her scripting columns.

E-Chapter

E-Handbook

0 comments

E-Mail

Username / Password

Password

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy