In this section, you create a dictionary and then populate it with a list of file names provided by the file system object.
1. | Open the \My Documents\Microsoft Press\VBScriptSBS\Templates\BlankTemplate.vbs template in a script editor. Save it as YourNameDictionary.vbs.
|
2. | On the first line, type Option Explicit.
|
3. | Declare the following variables by using the Dim command:
Dim objDictionary 'the dictionary object
Dim objFSO 'the FileSystemObject object
Dim objFolder 'created by GetFolder method
Dim colFiles 'collection of files from Files method
Dim objFile 'individual file
Dim aryKeys 'array of keys
Dim strKey 'individual key from array of keys
Dim strFolder 'the folder to obtain listing of files
|
4. | In the Reference section of the script, assign a folder to the strFolder variable. I used c:\windows, but you can use any folder you have rights to access. This is illustrated below:
strFolder = "c:\windows" <'>Ensure correct path
|
5. | Use CreateObject to create the dictionary. Assign it to the objDictionary variable, as seen below:
Set objDictionary = CreateObject("scripting.dictionary")
|
6. | Use CreateObject to create the file system object and assign it to the variable objFSO:
Set objFSO = CreateObject("Scripting.FileSystemObject")
|
7. | Use the GetFolder method to create a folder object. Use GetFolder to retrieve the folder represented by the strFolder variable. Assign it to the variable objFolder:
Set objFolder = objFSO.GetFolder(strFolder)
|
8. | Use the Files method of the Folder object represented by the objFolder variable. Assign it to colFiles, as seen below:
Set colFiles = objFolder.Files
|
9. | Use For Each to iterate through colFiles:
For Each objFile In colFiles
|
10. | Use the Add method of the Dictionary object to add the file name and the file size to the dictionary. The file name will be the key, and the file size will be the item associated with the key. This is seen below:
objDictionary.Add objFile.Name, objFile.Size
|
11. | Close out the For Each...Next loop by using the Next statement.
|
12. | Assign aryKeys to the Keys array of the dictionary that gets created by using the Keys method.
aryKeys = objDictionary.Keys
|
13. | Use For Each to iterate through the collection of keys:
For Each strKey In colKeys
|
14. | Echo out the file name and the file size:
WScript.Echo "The file: " & strKey & " is: " & _
objDictionary.Item(strKey) & " bytes"
|
15. | Close out the For Each...Next construction by typing Next.
|
16. | Add a header to the report of files that are contained in the directory listing. Do this by using the strFolder variable, which contains the name of the folder being reported. This line will need to go before the For Each...Next construction and is seen below:
WScript.Echo "Directory listing of " & strFolder
|
17. | On the next line, use the Count property of the Dictionary object to list the number of files in the folder. This is seen below:
WScript.Echo "***there are " & objDictionary.count & " files"
|
18. | Save and run your work using Cscript. If it does not run properly, compare your script with \My Documents\Microsoft Press\VBScriptSBS\ch05\OneStepFurther\Dictionary.vbs.
|
To | Do This |
---|
Use a string to populate an array | Use the Split function to turn the string into an array |
Resize a dynamic array | Use the ReDim command |
Resize a dynamic array and keep the existing data in it | Use the ReDim command with the Preserve keyword |
Change the way string values are compared in a Dictionary object | Change the Compare Mode property of the dictionary object |
Create a Dictionary object | Use the createObject command and specify the scripting.dictionary program ID |
Determine how many items are in the dictionary | Use the Count property |
Determine if an item exists in the dictionary prior to adding it | Use the Exists method |
Obtain a collection of keys from the dictionary | Use the Keys method |