9.14. FileSystem Object
The default WSH scripting languages
(VBScript and JScript) do not have any native file manipulation
capabilities. This functionality is provided by the COM object
FileSystem.The FileSystem object (FSO) exposes a number of
separate objects that provide the ability to perform file-related
operations. Table 9-18 lists the objects exposed by
the FileSystem component
Object | Description |
---|---|
Folders | A collection that contains a list of folders for a specified folder |
Folder | Exposes folder information such as size, attributes, and date information and methods to move, delete, and copy |
Files | A collection that contains a list of files for a specified folder |
File | Exposes file information such as size, attributes, and date information and methods to move, delete, and copy |
Drives | Collection object that contains a list of available drives on the local machine |
Drive | Exposes drive information, such as size and type |
TextStream | Provides text file manipulation |
operations. Before you can reference any of the objects listed in
Table 9-18, you must create an instance of the FSO
object:
'create an instance of an FSO objectThe FSO object exposes a number of file manipulation methods and
Set objFSO = CreateObject("Scripting.FileSystemObject")
properties. The following commands are a few of the most useful
FSO-related methods and properties. See "Additional
Resources," earlier in this chapter, for additional
documentation. The book Windows Me Annoyances
(O'Reilly) provides a great deal of ready-to-use
subroutines that perform all sorts of FSO-based file operations.
9.14.1. GetTempName Method
The GetTempName method generates a temporary
filename.
strTempName = objFSO.GetTempName( )
9.14.2. FileExists/FolderExists Methods
The FileExists /FolderExists methods
return True if the specified path exists, False otherwise.
9.14.3. Drives Collection
The Drives
collection contains a list of drives
available to the local machine. This includes any drive visible to
the system, including fixed hard, removable floppy, CD-ROM, or
network drives. This collection is returned from the FSO object. The
information for each object in the collection is exposed as a
Drive object.
Set objFSO = CreateObject("Scripting.FileSystemObject")Table 9-19 contains
Set objDrive = objFSO.Drives(0) 'get the first drive in collection
Set objDrive = objFSO.Drives("C") 'get a reference to the C drive
Drive object properties. All properties are
read-only unless otherwise specified.
drive:
Dim objFSO, objDrive
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
For Each objDrive In objFSO.Drives
'check if drive is ready
If objDrive.IsReady( ) Then
Wscript.Echo objDrive.Name & " is " & _
Fix(((objDrive.TotalSize - objDrive.FreeSpace) _
/ objDrive.TotalSize) * 100) & "% used"
Else
Wscript.Echo objDrive.Name & " is not ready"
End If
Next
9.14.4. Folders Collection
The Folders
collection contains list of a
Folder objects for a specified folder. To get a
reference to a Folders collection, use the FSO
object's GetFolder method to
return a Folder object, and then use the
folder's SubFolders property to
return the Folders collection. Its syntax is:
Set objFolder = objFSO.GetFolder(strFolderPath)The strFolderPath parameter identifies
which folder to reference.Table 9-20 lists Folder object
properties. Properties are read-only unless otherwise indicated.
Dim objFSO, objFolder, objSubThe folder object provides methods to copy, move, and delete a
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Windows")
For each objSub In objFolder.SubFolders( )
Wscript.Echo "Folder " & objSub.Name & " is " & objSub.Size _
& " bytes"
Next
Folder object.
9.14.5. Copy/Move Methods
The Copy
and
Move methods copy or move the contents of the
Folder object, including all subfolders and their
contents, to a specified destination folder:
objFolder.Copy strDestination, [bOverWriteFiles]Both methods require a destination folder parameter. The
objFolder.Move strDestination
Copy method has an optional
bOverWriteFiles parameter. This parameter is set
to True by default, but if it is set to False, the copy operation
will generate an error if a file already exists in the destination
folder.
Dim objFSO, objFolder, objSub
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\LocalData")
'copy items from folder to network folder
objFolder.Copy("H:\Data")
9.14.6. Delete Method
The Delete
method deletes the folder object and
its contents. The folder does not have to be empty, and any
subfolders and their contents will be deleted when the method is
invoked:
objFolder.Delete [bForce]The Delete method has an optional
bForce parameter that will attempt to
delete the folder and its contents if there is a situation such as a
locked file that prevents it from being deleted normally. However,
there are situations even when using the
bForce parameter where the folder and its
contents will not be deleted.
9.14.7. Files Collection
The Files
collection contains a
list of File objects for a specified folder:
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")Once you have a Files collection, you can
Set objFolder = objFSO.GetFolder("C:\Windows")
Set objFiles = objFolder.Files
enumerate the individual
File objects in the collection. Table 9-21 lists File object
properties. The properties are read-only unless otherwise indicated.
referenced using the FSO object's
GetFile method:
For each objFile In objFolder.Files( )
Wscript.Echo objFile.Name & " is " & objFile.Size & " bytes"
Next
9.14.8. Copy/Move Methods
The Copy and Move methods copy
or move the file to a specified destination folder. (This is the same
method for copying or moving folders; it is described here a second
time in the context of manipulating files instead of folders.) These
methods use the following syntax:
objFolder.Copy strDestination, [bOverWriteFiles]Both methods require a destination folder parameter. The
objFolder.Move strDestination
Copy method has an optional
bOverWriteFiles parameter. This parameter
is set to True by default, but if it is set to False, the copy
operation will generate an error if a file already exists in the
destination folder.
Dim objFSO, objFolder, objSub
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\LocalData")
'copy items from folder to network folder
objFolder.Copy("H:\Data")
9.14.9. Delete Method
The Delete
method deletes a file:
objFolder.Delete [bForce]The Delete method has an optional
bForce parameter that will attempt to
force a delete of the file if there is a situation such as a locked
file that prevents it from being deleted normally. Using
bForce does not guarantee that a locked
file will be deleted.