Lecture 6

Manage Folders/Files Using FSO

Welcome to our Lecture 6:)



When writing scripts for Windows Script Host, Active Server Pages, or other applications where scripting can be used, it often requires to operate folders/files, such as adding, moving, changing, creating, or deleting folders and files. In this lecture, we will learn how to use the FileSystemObject (FSO) Library to operate drives, folders and files.


Key words


FSO (FileSystemObject), FSO library, Scripting Runtime Library, WBEM (Web-Based Enterprise Management), File System, Folder Manipulation, File Manipulation


Reference to textbook chapters


This lecture covers Chapters 12 & 13. (Don Jones, VBScript, WMI and ADSI unleashed : using VBSscript, WMI, and ADSI to automate Windows administration eBook: Chapter 12. Working with the File System; Chapter 13. Putting It All Together.).



The FileSystemObject Library


FSO library contains a set of objects for variety of file operations, which allows you to create, delete, gain information about, and generally manipulate drives, folders, and files. Before we get into any details, let's practise the following script createFile.vbs.


Dim oFSO

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set a = oFSO.CreateTextFile("c:\\testfile.txt", true)

a.WriteLine "This is a test."

a.Close


The code simply creates a text file and writes a message to the file. In general, to program with the FileSystemObject (FSO) object library, we need to consider the following three scripting steps:

  1. use the CreateObject method to create a FileSystemObject object

  2. use the appropriate method on the newly created object

  3. access the object's properties.


To create a FileSystemObject object, simply use the following statement.

    CreateObject("Scripting.FileSystemObject")


Note that you need to use Set to assign the object to a variable. Once you get the object, you can use the following method to create text files, create folders, delete files, and delete folders:

  1. CreateTextFile, CreateFolder

  2. GetFile, GetFolder, GetDrive

  3. MoveFile, MoveFolder

  4. CopyFile, CopyFolder

  5. DeleteFile, DeleteFolder

See Working with Drives and Folders from Micrisoft Developer Network for the details of how to use these methods. See FileSystemObject Methods for a full list of the methods.


Operating Drives


The following code shows you how to get drive information programmatically:


Dim oFSO, oDrive

Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")

For Each oDrive In oFSO.Drives

    IF oDrive.isReady THEN

         MsgBox "Drive " & oDrive.DriveLetter & _

         " has a capacity of " & oDrive.TotalSize & " bytes " & _

         " and is drive type " & oDrive.DriveType

    END IF

Next


Note that oFSO.Drives contains all the drive objects mounted by the operating system that is running. Drive objects represent the logical drives attached to your system, including network drives, CD-ROM drives, and so forth. Therefore they can be many.


Each drive object contains the following data members:


  1. AvailableSpace

  2. DriveLetter

  3. DriveType

  4. FileSystem

  5. FreeSpace

  6. IsReady

  7. Path

  8. RootFolder

  9. SerialNumber

  10. ShareName

  11. TotalSize

  12. VolumeName 


The following code shows you how to operate a particular drive by providing a drive path drvPath


      Dim fso, d, s, drvPath

   'give your drive path

   drvPath = ???

   Set fso = CreateObject("Scripting.FileSystemObject")

   Set d = fso.GetDrive(fso.GetDriveName(drvPath))

   s = "Drive " & UCase(drvPath) & " - "

   s = s & d.VolumeName   &

   s = s & "Free Space: " & FormatNumber(d.FreeSpace/1024, 0)

   s = s & " Kbytes"

   MsgBox s


Drives also provide an entry point into each drive's file system, starting with the root folder of the file system hierarchy. Because the Drive object represents one of the simplest aspects of the file system, it's one of the simplest objects in the FSO.


Operating Folders


FSO offers more methods to the Folder object for manipulating folders than the Drive object:


  1.   CopyFolder: copies a folder.

  2.   CreateFolder: creates a new folder.

  3.   DeleteFolder: removes a folder permanently. Note that the deleted folder doesn't ever make it to the Recycle Bin, and there's no "Are you sure?" prompt.

  4.   FolderExists:, like DriveExists, returns a True or False indicating whether the specified folder exists.

  5.   GetFolder: accepts a complete folder path and, if the folder exists, returns a Folder object that represents the folder.

  6.   GetParentFolderName: accepts a complete folder path and returns the name of its parent folder.

  7.   GetSpecialFolder: returns the complete path to special operating system folders.

  8.   MoveFolder: moves a file a folder.


Download the program FolderOperation.vbs to practice.


Similar to drive objects, once you call GetFolder method of FSO, you get a folder object. Each folder object have the following methods:

  1.   Copy

  2.   Delete

  3.   Move

  4.   CreateTextFile


Using these methods is straightforward.


You can also use FSO to retrieve attribute information about folders.


Operating Files:


File operation is also similar. Download the program fileOperatrion.vbs to practice. Note that a file object contains the following properties:

  1. Attributes

  2. DateCreated

  3. DateLastAccessed

  4. DateLastModified

  5. Drive

  6. Name

  7. ParentFolder

  8. Path

  9. ShortName

  10. Size

  11. Type


Surprisingly, not only can you get the information about a file's or folder's attributes but also change these attributes.


Dim fso, f1, f2, s

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.GetFile("c:\test.txt")

f.attributes = 1


This code changes the attribute of C:\test.txt to read-only. See Attributes Property for the meaning of the attribute values.


Microsoft provides a comprehensive example code FileOperationCollection.vbs which can be run directly. It is very useful. We will practice it in the practical tasks.


At the end, I'd like to remind you again of Microsoft's Philosophy: Create a minimal core WSH structure that can interact with any number of separate object models for extention. For instance, Windows Management Instrumentation (WMI, See Lecture 5), the FileSystemObject (FSO, See Lecture 6), Active Directory Service Interfaces (ADSI, See Lecture 7), Collaboration Data Objects (CDO, See Lecture 8) object models are separate from the WSH object model (i.e. WMI, FSO, ADSI, and CDO are not built in WSH).