Insider Power Techniques for Microsoft Windows XP [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Insider Power Techniques for Microsoft Windows XP [Electronic resources] - نسخه متنی

Paul McFedries

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید




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



/ 126