1. | Open \My Documents\Microsoft Press\VBScriptSBS\Templates\BlankTemplate.vbs in Notepad or some other script editor and save the file as yourNamesbsCreateFolders.vbs.
|
2. | At the top of the script, set Option Explicit.
|
3. | Declare variables for the following: numFolders, folderPath, folderPrefix, objFSO, objFolder, i, objShell, and strDocPath. The Header section of your script will look like the following:
Option Explicit
Dim numFolders
Dim folderPath
Dim folderPrefix
Dim objFSO
Dim objFolder
Dim i
Dim objShell
Dim strDocPath
|
4. | Create an instance of the wshShell object. Use the variable objShell to hold the object that is returned. This line will look like the code below:
Set objShell = CreateObject("WScript.Shell")
|
5. | Use the strDocPath variable to hold the path that is obtained by using the SpecialFolders property of the wshShell object. This is seen below:
strDocPath = objShell.SpecialFolders("mydocuments")
|
6. | Assign a value of 10 to the variable numFolders.
|
7. | Use the folderPath variable to hold strDocPath concatenated with a backslash. This is seen below:
folderPath = strDocPath & "\"
|
8. | Assign folderPrefix to be equal to "Student". (The quotation marks are required.) The Reference section of the script will look like the following:
Set objShell = CreateObject("WScript.Shell")
strDocPath = objShell.SpecialFolders("mydocuments")
numFolders = 10
folderPath = strDocPath & "\"
folderPrefix = "Student"
|
9. | Begin a For...Next loop that counts from 1 to numFolders. Use i for the counter variable, as seen below:
|
10. | Create an instance of the FileSystemObject and use the variable objFSO to hold the object. The code will look like the following:
Set objFSO = CreateObject("Scripting.FileSystemObject")
|
11. | Use the FolderExists method to check for the existence of the folder prior to creating it. If the folder exists, echo out the path and state that it is not created. The code for this will look like the following:
If objFSO.FolderExists(folderPath & folderPrefix & i) Then
WScript.Echo(folderPath & folderPrefix & i & " exists." _
& " folder not created")
|
12. | If the folder does not exist, you will need to create it. To do this, build the path and the prefix. Then increment the i counter. The code will look like the following:
Else
Set objFolder = objFSO.CreateFolder(folderPath & folderPreFix & i)
|
13. | Echo out the folder path, prefix, and counter. Then state that the folder was created. The code will look like the following:
WScript.Echo(folderPath & folderPrefix & i & " folder created")
|
14. | Use End If to close out the If...Then section.
|
15. | Use Next to close out the For...Next loop.
The completed code follows:
Option Explicit
Dim numFolders
Dim folderPath
Dim folderPrefix
Dim objFSO
Dim objFolder
Dim i
Dim objShell
Dim strDocPath
Set objShell = CreateObject("WScript.Shell")
strDocPath = objShell.SpecialFolders("mydocuments")
numFolders = 10
folderPath = strDocPath & "\"
folderPrefix = "Student"
For i = 1 To numFolders
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(folderPath & folderPrefix & i) Then
WScript.Echo(folderPath & folderPrefix & i & " exists." _
& " folder not created")
Else
Set objFolder = objFSO.CreateFolder(folderPath & folderPreFix & i)
WScript.Echo(folderPath & folderPrefix & i & " folder created")
End If
Next
|