Previous Section Table of Contents Next Section

Working with Files

Files, of course, are the most granular object you can work with inside the FSO, and they're relatively uncomplicated. As with Drive and Folder objects, the FSO itself has some useful methods for working with files:

  • CopyFile

  • DeleteFile

  • FileExists

  • GetFile

  • MoveFile

These all work similarly to their Folder object counterparts, allowing you to obtain a reference to a file (GetFile), check for a file's existence (FileExists), and copy, delete, and move files. You can also create files, which is a process I'll cover a bit later in this chapter.

Working with File Objects

File objects themselves have a few methods.

  • Copy copies a file.

  • Delete removes a file without warning and without using the Recycle Bin.

  • Move moves a file.

  • OpenAsTextStream opens a file for reading (which I'll cover in the next section).

Properties of the File object include

  • Attributes

  • DateCreated

  • DateLastAccessed

  • DateLastModified

  • Drive

  • Name

  • ParentFolder

  • Path

  • ShortName

  • Size

  • Type

These all work identically to their Folder object property counterparts, which I covered in the previous section. The Type property can return different values for a file; use Listing 12.2 with different files to see what you get back. For example, for a file with a TXT filename extension, you should get something like "Text Document" from the Type property.

graphics/arrow.gif File Properties and Methods

Listing 12.4 shows an example of the File object's properties and methods in use.

Listing 12.4. FileProperties.vbs. This script uses both File and Folder objects to demonstrate various properties and methods.

Dim oFSO, oFolder, oFile, oNewFolder

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



Dim sPath

sPath = InputBox("Provide starting folder path")



If oFSO.FolderExists(sPath) Then



 Set oNewFolder = _

  oFSO.CreateFolder(oFSO.BuildPath(sPath,"Copies"))



 For Each oFile in oFolder.Files

  oFile.Copy oNewFolder.Path

  MsgBox "File " & Name & " last changed on " & _

   oFile.DateLastModified & " and of type " & _

   oFile.Type & ". It is contained in folder " & _

   oFile.ParentFolder.Path & " and uses the short " & _

   " filename " & oFile.ShortName & "."

 Next

End If



MsgBox "All Done!"

This script is ready to execute as-is on any system.

graphics/arrow.gif File Properties and Methods-Explained

This is a straightforward script. It starts by setting up some variables and creating an FSO.


Dim oFSO, oFolder, oFile, oNewFolder

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

Next, the script asks you to provide a starting path. An If…Then construct is used to perform the rest of the script's work only if the folder you provide actually exists.


Dim sPath

sPath = InputBox("Provide starting folder path")



If oFSO.FolderExists(sPath) Then

Next, the script uses the FSO's CreateFolder method to create a new folder. This method actually returns a Folder object referencing the new folder. Notice the BuildPath method, which is used to create an appropriate path string including the correct backslashes. I'll cover BuildPath in more detail later in this chapter.


Set oNewFolder = _

 oFSO.CreateFolder(oFSO.BuildPath(sPath,"Copies"))

Next, the script uses a For Each…Next construct to loop through each file in the folder that you specified. For each one, it copies the file into the newly created folder, using the Copy method of the file. Finally, it uses several of the File object's properties to display information about the file.


For Each oFile in oFolder.Files

 oFile.Copy oNewFolder.Path

  oFile.DateLastModified & " and of type " & _

 MsgBox "File " & Name & " last changed on " & _

  oFile.Type & ". It is contained in folder " & _

  oFile.ParentFolder.Path & " and uses the short " & _

  " filename " & oFile.ShortName & "."

Notice in particular the use of the ParentFolder property. This property actually represents a Folder object, with all of the properties and methods-including the Path property-of any Folder object. oFile.ParentFolder.Path is using the Path property of a Folder object-specifically, the folder that contains the file referenced by oFile.

The script finishes up by closing loops and constructs and displaying a message.


 Next

End If



MsgBox "All Done!"

This example should help you see how various properties and methods of the File object can be used, particularly those properties that are actually object references, such as ParentFolder.

    Previous Section Table of Contents Next Section