Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Copying A File

You can copy files using the Visual Basic FileSystemObject. This object provides you with access to the computer’s file system and has methods like CopyFile to copy a file:

FileSystemObject.CopyFile  source, destination [,  overwrite]

Here, source is the source file name (including path), destination is the destination file name (also including path), and overwrite is a Boolean that, if True, means you want to overwrite the destination file if it already exists. You can use wildcards (in other words, the asterisk [*]).

CopyFile solves a tedious problem for the programmer—if all you want to do is copy a file, why should you have to write all the code specifically to do that? You don’t, using CopyFile . Here’s an example where we copy a file, file.txt, to file2.txt. Notice that we must first create a FileSystemObject:

Private Sub Command1_Click()
   Dim FileSystemObject As Object
   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   FileSystemObject.CopyFile "c:\file.txt", "c:\file2.txt"
End Sub

You can also do the same thing with the Visual Basic FileObject, where we use GetFile to get a FileObject object and then use the FileObject’s Copy method:

Private Sub Command1_Click()
   Dim FileSystemObject, FileObject As Object
   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   Set FileObject = FileSystemObject.GetFile("c:\file.txt")
   FileObject.Copy "c:\file2.txt"
End Sub

Moving A File

The Visual Basic FileSystemObject lets you move a file from one directory to another using its MoveFile method. This method takes only two arguments, the source and destination paths. Here’s an example where we move a file, file.txt, from the C: to the D: drive; note that we must first create a FileSystemObject:

Private Sub Command1_Click()
   Dim FileSystemObject As Object
   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   FileSystemObject.MoveFile "c:\file.txt", "d:\file.txt"
End Sub

You can also do the same thing with the Visual Basic FileObject, where we use GetFile to get a FileObject and then use the FileObject’s Move method:

Private Sub Command1_Click()
   Dim FileSystemObject, FileObject As Object
   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   Set FileObject = FileSystemObject.GetFile("c:\file.txt")
   FileObject.Move "d:\file.txt"
End Sub

Deleting A File

The Visual Basic FileSystemObject lets you delete a file using its DeleteFile method:

FileSystemObject.DeleteFile   filespec [, force]

Here, filespec is the file you want to delete, and force is a Boolean that, if True, means you want to delete read-only files as well. Let’s see an example. Here, we delete a file, file.txt, from the C: drive; note that we must first create a FileSystemObject:

Private Sub Command1_Click()
   Dim FileSystemObject As Object
   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   FileSystemObject.DeleteFile "c:\file.txt"
End Sub

You can also do the same thing with the Visual Basic FileObject, where we use GetFile to get a FileObject and then use the FileObject’s Delete method:

Private Sub Command1_Click()
   Dim FileSystemObject, FileObject As Object
   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   Set FileObject = FileSystemObject.GetFile("c:\file.txt")
   FileObject.Delete
End Sub

When Was A File Created? Last Modified? Last Accessed?

You can use Visual Basic FileObject to determine when a file was created, last modified, and last accessed. The properties that are important here are DateCreated, DateLastModified, and DateLastAccessed.

Let’s see an example. Here, we use a multiline (that is, MultiLine = True) text box, Text1, to display when a file, file.dat, was created, last modified, and last accessed. First, we get a FileObect for that file:

Private Sub Command1_Click()
   Dim FileSystemObject, FileObject As Object
   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   Set FileObject = FileSystemObject.GetFile("c:\file.dat")
...

The we display the file’s created, last modified, and last accessed dates in the text box:

Private Sub Command1_Click()
   Dim FileSystemObject, FileObject As Object
   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   Set FileObject = FileSystemObject.GetFile("c:\file.dat")
   Text1.Text = "c:\file.dat:" & vbCrLf & "was created " & _
       FileObject.DateCreated & vbCrLf & "was last modified: " & _
       FileObject.DateLastModified & vbCrLf & "was last accessed: " & _
       FileObject.DateLastAccessed & vbCrLf
End Sub

The result of this code appears in Figure 17.14. Using the FileObject, you can find out quite a bit of information about a file.


Figure 17.14  Displaying a file’s creation, last modified, and last accessed dates.

Creating A TextStream

You can use TextStream objects to work with text files in Visual Basic. We’ll see how to work with TextStream objects in the next few topics in this chapter. For example, you create a text stream with the CreateTextFile method:

FileSystemObject.CreateTextFile(  filename[, overwrite[,  unicode]])

Here’s what the arguments we pass to CreateTextFile mean:

  filename—String which identifies the file to create.
  overwrite—Boolean value that indicates if an existing file can be overwritten. The value is True if the file can be overwritten; False if it can’t be overwritten. If omitted, existing files are not overwritten.
  unicode—Boolean value that indicates whether the file is created as a Unicode or an ASCII file. The value is True if the file is created as a Unicode file; False if it’s created as an ASCII file. If omitted, an ASCII file is assumed.

Here’s an example where we create a TextStream object corresponding to a file named file.txt:

Private Sub Command1_Click()
   Dim FileSystemObject, TextStream As Object

   Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
   Set TextStream = FileSystemObject.CreateTextFile("c:\file.txt", True)
End Sub


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.