Previous Page
Next Page

File It Under Files

In your first file system script, ListFiles.vbs, connect to FileSystemObject, attach it to a folder defined by the variable FolderPath, and then use the Files command to enable the For Each loop to echo out each file in the folder. This is just the beginning of what can be done with this script. Continue to think of ways to expand this script so that you can perform some really useful network administration tasks.

ListFiles.vbs

Option Explicit
On Error Resume Next
Dim FolderPath        'path to the folder to be searched for files
Dim objFSO            'the FileSystemObject
Dim objFolder         'the folder object
Dim colFiles          'collection of files from files method
Dim objFile           'individual file object

FolderPath = "c:\fso"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(FolderPath)
Set colFiles = objFolder.Files

For Each objFile in colFiles
  WScript.Echo objFile.Name, objFile.Size & " bytes"
  WScript.Echo VbTab & "created: " & objFile.DateCreated
  WScript.Echo VbTab & "modified: " & objFile.DateLastModified
Next

Header Information

In the Header information section of ListFiles.vbs are the normal Option Explicit and On Error Resume Next commands. These are used to specify the declaration of all variables and to provide rudimentary error suppression. Next, five variables will need to be declared. These variables and the description of their use are listed in Table 6-1.

Table 6-1. Variables used in ListFiles.vbs

Variable name

Use

FolderPath

Defines the folder to be enumerated in the script

objFSO

Creates FileSystemObject

objFolder

Holds the connection to the folder whose path is stored in the FolderPath variable. The connection is returned by the GetFolder method of FileSystemObject

colFiles

Holds the collection of files returned by using the Files method

objFile

Holds individual files as the script iterates through the collection of files by using the For Each construction


For more information about using the Option Explicit and On Error Resume Next commands, see Chapter 1, "Starting from Scratch."

Reference Information

The Reference information section of the ListFiles.vbs script is similar to other scripts. Assign a value to the FolderPath variable created in the Header information section. The FolderPath variable is used to make the script easier to modify in the future. By changing the path contained in the FolderPath variable, the script can list files on any machine. In addition, FolderPath provides a great deal of flexibility.

With just a little work, ListFiles.vbs can be modified to take command-line input or to find the value for FolderPath by reading a list of paths from a text file. Perhaps a more intriguing way of obtaining the folder path is to use the BrowseForFolder method from the Shell.Application object we used in Chapter 1. The graphical tool created by this method is seen in Figure 6-1.

The complete Reference information section follows:

FolderPath = "C:\fso"

Figure 6-1. Use BrowseForFolder to obtain the folder path


Worker and Output Information

The Worker and Output information section of the ListFiles.vbs script first creates the file sytem object and assigns it to the objFSO variable. objFSO is used to hold the instance of FileSystemObject that comes back from the CreateObject command. By using the CreateObject("Scripting.FileSystemObject") command, you can work with the file system to enumerate all the files in the folder.

The folder from which files are listed is defined by using the GetFolder method. The variable objFolder is used to hold the copy of the folder object that is created by using the GetFolder method of the FileSystemObject as seen in the code objFSO.GetFolder(FolderPath). FolderPath is the variable that is used to hold the path to the folder whose contents we want to enumerate.

Once connected to the folder, you use the Files method to get a list of files contained in the folder. Assign this list of files to the colFiles variable by using the following code:

Set colFiles = objFolder.Files

Next, use a For Each...Next loop to walk through the collection of files returned by the File method. The WScript.Echo command is used to display the file name and the file size. The complete Worker and Output information is seen below:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(FolderPath)
Set colFiles = objFolder.Files

For Each objFile in colFiles
  WScript.Echo objFile.Name, objFile.Size & " bytes"
Next

Browse for a folder and list file properties

1.
Open the \My Documents\Microsoft Press\VBScriptSBS\ch06\ListFiles.vbs script in Microsoft Notepad or your favorite script editor and save it as YourNameBrowseFolderListFiles.vbs.

2.
Declare a new variable that will be used to hold the output from script. Call it strOUT, as seen below:

Dim strOUT 'single output variable

3.
Open CheckForWScript.vbs from the Utilities folder and copy the subroutine to the bottom of your script. The sub looks like the following:

Sub subCheckWScript
If UCase(Right(WScript.FullName, 11)) = "CSCRIPT.EXE" Then
  WScript.Echo "This script must be run under WScript."
  WScript.Quit
End If
End Sub

4.
Open the BrowseFolderSub.vbs script from the Utilities folder and copy the subroutine to the bottom of your script. The sub looks like the following:

Sub subGetFolder
Dim objShell, objFolder, objFolderItem, objPath
Const windowHandle = 0
Const folderOnly = 0
const folderAndFiles = &H4000&

Set objShell = CreateObject("Shell.Application")
Set objFOlder = objShell.BrowseForFolder(windowHandle, _
   "Select a folder:", folderOnly)
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
End Sub

5.
Call the subCheckWScript subroutine, then call the subGetFolder subroutine. Place these two calls to the subroutines directly under the variable Declarations in the Header section of the script. This is seen below:

subCheckWScript'ensures script is running under WScript
subGetFolder'calls the BrowseForFolder method

6.
Save your script, but do not run it yet, because it will result in errors.

7.
Go back to the subGetFolder subroutine and in the line that builds the path, change the objPath variable to FolderPath. Delete objPath from the Declarations section in the subroutine as well. The modified line looks like the following:

FolderPath = objFolderItem.Path

8.
Inside the For Each...Next loop in the Worker section of the main script, modify the output so that instead of making a series of WScript.Echo boxes, it will not make any. This will enable you to capture the output into a single variable. This facilitates future modification, by having all the output in a single variable. To do this, set the strOUT variable to be equal to itself and the two properties. Add VbCrLf at the end of the line to list each file name and size on an individual line. This is seen below:

For Each objFile in colFiles
  strOUT = strOUT & objFile.Name & VbTab & objFile.Size _
  & " bytes" & VbCrLf
Next

9.
After the For Each...Next loop, use WScript.Echo to print out the strOUT variable. This is seen below:

WScript.Echo strOUT

10.
In the Reference section of the script, delete FolderPath = "c:\fso" because FolderPath is assigned its value in the subGetFolder subroutine.

11.
Save and run the script under WScript. It should produce a single output box listing the files and their size. If it does not, compare your script to the \My Documents\Microsoft Press\VBScriptSBS\ch06\BrowseFolderListFiles.vbs script.

Quick Check

Q.

What is required to talk to the file system by using FileSystemObject?

A.

You can use FileSystemObject by first using the CreateObject command, and then assigning to a variable the object that comes back.

Q.

Why do you want an object for FileSystemObject?

A.

You want a object for FileSystemObject because it enables you to work with files and folders.



Previous Page
Next Page