| Answer: |
The FileSystemObject contains a CreateFolder method that can be used to create a folder on the Web server's file system. This method takes one parameter, the path of the folder to create. For example, to create a FooBar folder on the C:\ drive, you could use the following code:
'Create an instance of the FileSystemObject Dim objFSO Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Create C:\FooBar objFSO.CreateFolder("C:\FooBar")
|
It's that simple! One caveat: if the folder already exists and you try creating it, you will get a "File Already Exists" error. Therefore, rather than just blindly adding a new folder, you may wish to check if it exists first and, if it doesn't, then add it. You can check to see if a folder exists using the FolderExists method of the FileSystemObject object like so:
'Create an instance of the FileSystemObject Dim objFSO Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Create C:\FooBar If Not objFSO.FolderExists("C:\FooBar") then objFSO.CreateFolder("C:\FooBar") End If
|
This script first checks to make sure the folder doesn't exist and then adds it. This method is preferred since it will not try to create a folder if it already exists.
A closing note: when dealing with IIS, the permissions granted to IUSR_machinename are important. For example, if you do not have write permission to the C:\ drive for IUSR_machinename, an error will occur in the above script. (The machinename part of the IUSR_machinename bit above is the name of the Web server. If the Web server's machine name is Bob, then the IUSR_Bob account must have adequate permissions.)
-- View the technical docs for CreateFolder -- View the technical docs for FolderExists
Happy Programming! |