AOL
  AT HOME
  INTEL
 MICROSOFT
 COMPAQ
 YAHOO
 ORACLE
 DELL
 INTUIT
 

PC Magazine

  PC Tech

File Management via Windows Scripting

Introduction

File Management Objects

A General-Purpose File-Deletion Script

A Few Details

Figure 1: File deletion script.

Figure 2: VBScript's File-Handling Objects



X10.com - The SuperSite for Home Automation!

NextCard Internet Visa - Apply Now

 
  Categories
Scripting Languages
Windows 95/NT
Operating Systems

Related Stories:
The Windows Scripting Host
-- Operating Systems 2/10/98

File Management via Windows Scripting
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.


 SPONSORED LINKS
@Backup   Your Solid Online Backup Plan. Download Now.
Services   9c/MINUTE LONG DISTANCE, 5c/MINUTE ON SUNDAYS!
STORAGE   Quantum means non-stop business, 24 hours a day
Software   X10.com -- The SuperSite for Home Automation
Books   Bargain Books up to 90% off at barnesandnoble.com
 ZDNET FEATURED LINKS
Downloads   Check out the best new downloads in Reviewer's Raves
Bargains!   Shop the Basement for best buys on computer products
Free Help   Got computing questions? ZDHelp has all the answers!
 MAGAZINE OFFERS
Free Offer   Get a FREE SUBSCRIPTION to Inter@ctive Week

TOP
Copyright (c) 1998 Ziff-Davis Inc.