File It Under FilesIn 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 InformationIn 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.
For more information about using the Option Explicit and On Error Resume Next commands, see Chapter 1, "Starting from Scratch." Reference InformationThe 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 InformationThe 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
![]() |