|
|
File Management Objects Continued from Introduction Four file-handling objects are supplied with the scripting engine: a FileSystemObject object, a Folder object, a File object, and a Drive object, which we won't cover in this article. The properties and methods of these objects are exactly what you need to take advantage of the file-handling capabilities in the Windows scripting engine. (For those who have never programmed an object-based language like Visual Basic before, think of properties as describing what an object is and methods as describing what it can do. For example, Name is a property of a File object, while Delete is a method of it.) A table that lists the properties and methods of the three file-handling objects we use in this article, along with brief explanations of them, can be found on PC Magazine Online. If you'd like more details on these properties and methods, you can download the VBScript documentation from www.microsoft.com/scripting. You always get Folder and File objects via a FileSystemObject, which you get using a call to the CreateObject function in VBScript together with the Set method: Set FSO = CreateObject("Scripting.FileSystemObject") (We need the Set statement instead of a simple "=" since we're setting a variable equal to an object, not to a basic type such as a number.) At this point you can use the FSO variable to gain access to the file system on the host computer. The following code, for example, lets you create a simple text file that contains one line of text: Set FSO = CreateObject("Scripting.FileSystemObject") Set TestFile = FSO.CreateTextFile("c:\testfile.txt", True) TestFile.WriteLine("This is a test file with one line in it.") TestFile.Close For our project we will be working with existing files, which requires using a property of either the FileSystemObject or the Folder object. The FileSystemObject's GetFile method will give you a File object associated with the designated file specification (either the filename alone, which will give a file in the current folder, or a full pathname.) The following code lets you get at the various attributes of a file whose file specification is AFileSpec in the current folder: Set FSO = CreateObject("Scripting.FileSystemObject") Set AFile = FSO.GetFile(AFileSpec) Message= AFile.Name & " on Drive " & UCase(AFile.Drive) & vbCrLf Message = Message & "Created " & AFile.DateCreated & vbCrLf Message = Message & "Last Accessed: " & AFile.DateLastAccessed _ & vbCrLf Message = Message & "Last Modified: " & AFile.DateLastModified MsgBox Message, 0, "File Info" Next: A General-Purpose File-Deletion Script Published as Operating Systems in the 6/30/98 issue of PC Magazine. |
|
TOP |
Copyright (c) 1998 Ziff-Davis Inc. |