Creating a Folder

Microsoft® Windows® 2000 Scripting Guide

It is unlikely that you will ever sit down, implement your file system infrastructure (that is, your folders and subfolders), and then never have to touch that infrastructure again. Instead, a file system tends to be dynamic: because of ever-changing needs, existing folders might be deleted and new folders might be created. For example, if your organization provides users with storage space on file servers, you need to create a new folder each time a new user account is created.

The FileSystemObject gives script writers the ability to programmatically create folders, a capability that can make your scripts even more powerful and more useful. For example, the script in Listing 4.6 checks to see whether a specified folder exists. If the folder exists, the script uses the GetFolder method to bind to the folder. If the folder does not exist, the script echoes a message to that effect.

Although this approach prevents the script from crashing, you might prefer that your script create the folder rather than simply report that the folder does not exist. To do this, create an instance of the FileSystemObject, and then call the CreateFolder method, passing the complete path to the new folder as the sole parameter. For example, the script in Listing 4.7 creates a new folder named C:\FSO.

Listing 4.7 Creating a New Folder

  
1
2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder("C:\FSO")

If the folder already exists, a "File exists" error will occur. Because of that, you might want to check for the existence of the folder before trying to create (or, in that case, re-create) it.

note Note

  • The FileSystemObject can only create folders on the local computer. If you need to create folders on a remote computer, you will need to use the WshController object. Alternatively, you can create a folder locally and then use WMI to move that folder to the remote computer. (The folder must be created and then moved because WMI does not have a method for creating folders.)