![]() |
Table of Contents |
![]() |
Working with FoldersFolders offer up a bit more complexity. First, the FSO itself offers more methods for manipulating specific folders.
The following example illustrates a few of these base functions. Dim oFSO Set oFSO = WScript.CreateObject("Scripting.FileSystemObject") Dim oFolder If oFSO.FolderExists("C:\MyFolder") Then Set oFolder = oFSO.GetFolder("C:\MyFolder") Else oFSO.CreateFolder "C:\MyFolder") Set oFolder = oFSO.GetFolder(C:\MyFolder") End If MsgBox oFSO.GetParentFolderName(oFolder.Path) This example creates a folder named C:\MyFolder, and then displays its parent folder, which of course is just C:\. Working with Folder ObjectsAlthough the FSO's base methods are useful for manipulating folders, folders themselves have a number of useful methods and properties that allow a more granular level of control. For example, Folder objects have four methods.
To illustrate these methods, I'll expand on the last example. Dim oFSO Set oFSO = WScript.CreateObject("Scripting.FileSystemObject") Dim oFolder If oFSO.FolderExists("C:\MyFolder") Then Set oFolder = oFSO.GetFolder("C:\MyFolder") Else oFSO.CreateFolder "C:\MyFolder") Set oFolder = oFSO.GetFolder(C:\MyFolder") End If oFolder.Copy "C:\MyOtherFolder" oFolder.Delete The result is a single folder named C:\MyOtherFolder. The operations of creating the new C:\MyFolder folder, copying it, and deleting it all occur almost instantly. Folder objects support a number of useful properties, as well:
Some of these properties are straightforward. For example, you can probably figure out what type of information the DateLastModified property will return, and you can guess what the Path property will display. A few of these properties, however, deserve further explanation. The Type property in particular is interesting. To see what it returns, try the example in Listing 12.2 (which will work for files and folders, both of which have a Type property). Try specifying the Recycle Bin or other special folders to see what you get. Listing 12.2. Types.vbs. Shows the type of a file or folder.Dim oFSO, oF Set oFSO = WScript.CreateObject("Scripting.FileSystemObject") Dim sPath sPath = InputBox("Enter the path to a file or folder.") If oFSO.FolderExists(sPath) Then Set oF = oFSO.GetFolder(sPath) ElseIf oFSO.FileExists(sPath) Then Set oF = oFSO.GetFile(sPath) Else MsgBox "Can't find what you typed." WScript.Exit End If MsgBox oF.Type Folder AttributesThe Attributes property returns specific attributes of the folder, such as whether it is read only or compressed. These attributes are numeric, and because a folder can have many different attributes at once-such as both compressed and hidden-you have to manipulate the Attributes property a bit to figure out what's what. The possible values are
To figure out which attributes are turned on, you have to perform some Boolean math. Because you're a systems administrator, I'm going to assume that you don't really care for a detailed explanation of what Boolean math is or does, but that you probably just prefer to see an example of it in action. Listing 12.3 is just that. Listing 12.3. CheckFolder.vbs. Checks the attributes of a specified folderDim oFSO, sFolder, oFolder Set oFSO = WScript.CreateObject("Scripting.FileSystemObject") sFolder = InputBox("Full path of folder to check?") Set oFolder = oFSO.GetFolder(sFolder) Dim sMsg If oFolder.Attributes AND 0 Then sMsg = sMsg & "Folder is normal" & vbCrLf End If If oFolder.Attributes AND 1 Then sMsg = sMsg & "Folder is Read only" & vbCrLf End If If oFolder.Attributes AND 2 Then sMsg = sMsg & "Folder is Hidden" & vbCrLf End If If oFolder.Attributes AND 4 Then sMsg = sMsg & "Folder is a system folder" & vbCrLf End If If oFolder.Attributes AND 8 Then sMsg = sMsg & "Folder is really a volume" & vbCrLf End If If oFolder.Attributes AND 16 Then sMsg = sMsg & "Folder is a directory" & vbCrLf End If If oFolder.Attributes AND 32 Then sMsg = sMsg & "Folder has changed since the last backup" & vbCrLf End If If oFolder.Attributes AND 1024 Then sMsg = sMsg & "Folder is a shortcut" & vbCrLf End If If oFolder.Attributes AND 2048 Then sMsg = sMsg & "Folder is compressed" & vbCrLf End If MsgBox sMsg By using the Boolean AND operator to compare the Attributes property to the predefined values, you can figure out which attributes are turned on and which ones aren't. This script builds up a message in variable sMsg, which contains the status of the various attribute flags. Some of these attributes can be changed. You can use the Attributes property to alter the read-only status, the hidden status, the system status, and the archive status. You cannot change any of the other attributes. To set an attribute, use the AND operator again. 'Set the Read-Only status to be true oFolder.Attributes = oFolder.Attributes AND 1 'Now try turning on compression: oFolder.Attributes = oFolder.Attributes AND 2048 The last line of code causes an error, because the compression attribute is read-only within scripting, and cannot be changed by the FSO. Properties That Are ObjectsSome of a Folder object's properties are actually references to other objects.
The SubFolders property provides access to an object hierarchy that represents the folder hierarchy of the file system. Figure 12.1 illustrates the relationship between a Drive object (in this case, a network drive), its RootFolder property (which returns a Folder object), and that folder's SubFolders property (which returns a collection of Folder objects). Figure 12.1. The hierarchy of Drive and Folder objects in the FSO |
![]() |
Table of Contents |
![]() |