Section 9.14. FileSystem Object

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

Table 9-18. FileSystem objects

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


The FSO exposes methods and properties that perform 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 object Set objFSO = CreateObject("Scripting.FileSystemObject") 

The FSO object exposes a number of file manipulation methods and 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") Set objDrive = objFSO.Drives(0) 'get the first drive in collection  Set objDrive = objFSO.Drives("C") 'get a reference to the C drive 

Table 9-19 contains Drive object properties. All properties are read-only unless otherwise specified.

Table 9-19. Drive object properties

Property

Description

AvailableSpace ,

FreeSpace

Free space on drive in bytes. These properties return the same operation.

DriveLetter

Drive letter associated with drive.

DriveType

Type of drive: Unknown=0, Removable=1, Fixed=2, Remote=3, CD-ROM=4, RamDisk=5.

FileSystem

Filesystem drive utilities, e.g., FAT, NTFS, CDFS, etc.

IsReady

Returns True if drive is ready, False otherwise. Useful for removable media such as floppy and CD-ROM drives to determine if there is media in the drive.

RootFolder

Returns root folder for drive.

SerialNumber

Serial number uniquely identifying the drive.

ShareName

Displays share name if network drive.

TotalSize

Total size in bytes.

VolumeName

Name of drive. This is read/write, so you can set a drive's volume name.


The following example displays the percentage of space used by each 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.

Table 9-20. Folder properties

Attributes

Attributes

Attributes

Directory attributes. Numeric value is comprised of one or more directory attribute types. The following attribute values are added together to form the directory Attributes property and Read/Write property: Normal=0, Hidden=2, System=4, Volume=8, Archive=32, Compressed=2048.

DateCreated / DateLastAccessed / DateLastModifed

Returns file created, last accessed, and modified date for file.

Drive

Drive object folder resides on.

Files

Files collection containing all files in folder.

IsRootFolder

True if Folder object is folder directory root.

Name

Filename.

ParentFolder

Folder object with folder's parent folder.

Path

Full path to folder.

ShortName / ShortPath

DOS short name for folder and path, respectively.

Size

Size of all files and folders in folder, including all subfolders.

Type

Folder type, e.g., File or Subscription folder.


For example:

 Dim objFSO, objFolder, objSub 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 

The folder object provides methods to copy, move, and delete a 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   ] objFolder.Move   strDestination   

Both methods require a destination folder parameter. The 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") Set objFolder = objFSO.GetFolder("C:\Windows") Set objFiles = objFolder.Files 

Once you have a Files collection, you can enumerate the individual File objects in the collection. Table 9-21 lists File object properties. The properties are read-only unless otherwise indicated.

Table 9-21. File object properties

Attributes

Description

Attributes

File attributes. Numeric value comprised of one or more file attribute types. The attribute values are added together to form the directory Attributes property and the Read/Write property: Normal=0, Hidden=2, System=4, Volume=8, Archive=32, Compressed=2048.

DateCreated / DateLastAccessed / DateLastModifed

Returns file created, last accessed, and modified date for file.

Drive

Drive object file resides on.

Name

Filename.

ParentFolder

Folder object containing file's parent folder .

Path

Full path to file.

ShortName / ShortPath

DOS short name for folder and path, respectively.

Size

Size of all files and folders in folder, including all subfolders.

Type

File type description, e.g., Text Document.


The File object for an individual file can be 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   ] objFolder.Move   strDestination   

Both methods require a destination folder parameter. The 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.



Windows XP in a Nutshell
Windows XP in a Nutshell, Second Edition
ISBN: 0596009003
EAN: 2147483647
Year: 2003
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net