|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Copying A FileYou can copy files using the Visual Basic FileSystemObject. This object provides you with access to the computers 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 programmerif all you want to do is copy a file, why should you have to write all the code specifically to do that? You dont, using CopyFile . Heres 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 FileObjects 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 FileThe 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. Heres 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 FileObjects 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 FileThe 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. Lets 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 FileObjects 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. Lets 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 files 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.
Creating A TextStreamYou can use TextStream objects to work with text files in Visual Basic. Well 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]]) Heres what the arguments we pass to CreateTextFile mean:
Heres 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
|
|
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. |