Previous Section Table of Contents Next Section

Working with Folders

Folders offer up a bit more complexity. First, the FSO itself offers more methods for manipulating specific folders.

  • CopyFolder copies a folder.

  • CreateFolder creates a new folder.

  • DeleteFolder removes a folder permanently. Note that the deleted folder doesn't ever make it to the Recycle Bin, and there's no "Are you sure?" prompt.

  • FolderExists, like DriveExists, returns a True or False indicating whether the specified folder exists.

  • GetFolder accepts a complete folder path and, if the folder exists, returns a Folder object that represents the folder.

  • GetParentFolderName accepts a complete folder path and returns the name of its parent folder. For example, GetParentFolderName("C:\Windows\System32") would return "C:\Windows".

  • GetSpecialFolder returns the complete path to special operating system folders. For example, GetSpecialFolder(0) returns the path for the Windows folder. Use 1 for the System32 folder, and use 2 for the system's temporary files folder.

  • MoveFolder moves a file a folder.

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 Objects

Although 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.

  • Copy copies the folder. You just specify the destination for the copy. This method provides the same functionality as the FSO's CopyFolder method.

  • Delete mimics the FSO's DeleteFolder method. However, because you're using the folder's method directly, you don't have to specify which folder to delete.

  • Move mimics the FSO's MoveFolder method.

  • CreateTextFile returns a TextStream object and creates a new text file in the folder. I'll cover this functionality in the next section.

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:

  • Attributes

  • DateCreated

  • DateLastAccessed

  • DateLastModified

  • Drive

  • Files

  • IsRootFolder

  • Name

  • ParentFolder

  • Path

  • ShortName

  • ShortPath

  • Size

  • SubFolders

  • Type

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 Attributes

The 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

  • Normal: 0

  • Read-only: 1

  • Hidden: 2

  • System: 4

  • Volume: 8

  • Directory: 16

  • Archive: 32

  • Alias: 1024

  • Compressed: 2048

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 folder

Dim 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 Objects

Some of a Folder object's properties are actually references to other objects.

  • The Drive property returns a Drive object that represents the drive that contains the folder.

  • The Files property returns a collection of File objects, representing the files within the folder. I'll cover File objects in the next section.

  • The ParentFolder property returns a Folder object that represents the folder's parent folder. If the folder is the root folder, you cannot use ParentFolder because the root doesn't have a parent. Use the IsRootFolder property, which returns True or False, to figure out if the folder is the root or not.

  • The SubFolders property returns a collection of Folder objects, representing the folders contained within the folder.

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

graphics/12fig01.gif

    Previous Section Table of Contents Next Section