Programming the VBScript FileSystemObject


One of the most powerful uses for scripted Automation is accessing the object models exposed by the VBScript engine, particularly the FileSystemObject that gives you access to the local file system. This enables you to create scripts that work with files, folders, and disk drives; read and write text files; and more. You use the following syntax to refer to this object:

Scripting.FileSystemObject 

For all your file system scripts, you begin by creating a new instance of FileSystemObject:

Set objFS = WScript.CreateObject(“Scripting.FileSystemObject”)

Here’s a summary of the file system objects you can access via this Automation object:

  • Drive This object enables you to access the properties of a specified disk drive or network path. To reference a Drive object, use either the Drives collection (discussed next) or the FileSystemObject object’s GetDrive method. For example, the following VBScript statements reference drive C:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject") Set objDrive = objFS.GetDrive("C:")

  • Drives This object is the collection of all available drives. To create this collection, use the FileSystemObject object’s Drives property:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject") Set objDrives = objFS.Drives

  • Folder This object enables you to access the properties of a specified folder. To reference a Folder object, use either the Folders collection (discussed next) or the FileSystemObject object’s GetFolder method:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject") Set objFolder = objFS.GetFolder("C:\My Documents")

  • Folders This object is the collection of subfolders within a specified folder. To create this collection, use the Folder object’s Subfolders property:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject") Set objFolder = objFS.GetFolder("C:\Windows") Set objSubfolders = objFolder.Subfolders

  • File This object enables you to access the properties of a specified file. To reference a File object, use either the Files collection (discussed next) or the FileSystemObject object’s GetFile method:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject") Set objFile = objFS.GetFile("c:\boot.ini") 

  • Files This object is the collection of files within a specified folder. To create this collection, use the Folder object’s Files property:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject")  Set objFolder = objFS.GetFolder("C:\Windows") Set objFiles = objFolder.Files

  • TextStream This object enables you to use sequential access to work with a text file. To open a text file, use the FileSystemObject object’s OpenTextFile method:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject") Set objTS = objFS.OpenTextFile("C:\Autoexec.bat")

    Alternatively, you can create a new text file by using the FileSystemObject object’s CreateTextFile method:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject") Set objTS = objFS.CreateTextFile("C:\test.txt")

    Either way, you end up with a TextStream object, which has various methods for reading data from the file and writing data to the file. For example, the following script reads and displays the text from C:\Boot.ini:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject") Set objTS = objFS.OpenTextFile("c:\boot.ini") strContents = objTS.ReadAll WScript.Echo strContents objTS.Close




Insider Power Techniques for Microsoft Windows XP
Insider Power Techniques for Microsoft Windows XP (Bpg-Other)
ISBN: 0735618968
EAN: 2147483647
Year: 2005
Pages: 126

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