FSO Object Model in VB6

Source: Internet
Author: User
FSO Object Model in VB6

 

 

Yan bingshu

In programming, we often need to process the drives, folders, and files in the file system, such as collecting information about the drive, creating, adding, moving, or deleting folders and files. In VB6, a new object model named FSO (File System Object) is provided to access the file system. This model provides an object-based tool. Through a series of attributes and methods provided by it, we can perform various operations on the file system more easily and flexibly in the application.

I. FSO Introduction

The FSO object model contains the following objects:

Drive object: allows you to collect information about the available space, shared names, and other information of a drive, such as a hard disk, a CD-ROM, that is physically connected to the system through a LAN or a logical connection to the system.

Folder object: allows you to create, delete, or move folders, and query the folder name and path from the system.

Files object: allows you to create, delete, or move files, and query the file name and path from the system.

Textstream object: allows you to create and read/write text files.

FileSystemObject object: provides a complete set of methods for drive, folder, and file operations. It can be viewed as a collection of the above objects and is often used with them. Many methods associated with this object repeat the methods in the previous four objects. Therefore, we can perform most operations on the drive, folder, and file through the FileSystemObject object, you can also operate these components through the corresponding drive, folder or file object. The FSO model implements operations on the same object in two ways, with the same effect. The purpose of providing this redundancy function is to achieve maximum programming flexibility.

The FSO object model is included in a type library called scripting, which is located in scrrun. in the DLL file, you can select the Microsoft scripting runtime option from the reference dialog box in the project menu to reference this file.

To create a FileSystemObject object, you can use either of the following methods: 1. Declare a variable as a FileSystemObject object type: dim FSO as new FileSystemObject; 2. Create a FileSystemObject object using the Createobject method: set FSO = Createobject ("scripting. fileSystemObject ″).

2. operations on the drive

The drive object and FileSystemObject object are used to operate the drive. FSO does not support creating or deleting the drive, but only allows you to collect information about the drive used by the system. The following information can be obtained through the attributes of this object: The drive space (availablespace or freespace) in bytes; the letter (driveletter) specified for the drive; the drive type (drivetype ); the type of the file system used by the drive, such as fat, FAT32, NTFS (filesystem); whether the drive is available (isready); the drive path or root folder (path and rootfolder ); drive serial number; shared name or volume name (sharename and volumename); Total drive space in bytes (totalsize ). The following example shows how to collect information about the drive:

Private sub Command1-Click ()

Dim FSO as new FileSystemObject, DRV as drive, STR as string

Set DRV = FSO. getdrive (FSO. getdrivename ("C :″))

Debug. Print "total space:" & formatnumber (DRV. totalsize/1024, 0) & "kb ″

Debug. Print "Free Space:" & formatnumber (DRV. freespace/1024, 0) & "kb ″

End sub

3. Folder operations

The folder object allows you to copy, delete, or move a folder using the provided method, and query its name (name) from the system) path and datecreated. The FileSystemObject object can operate on folders through createfolder, copyfolder, movefolder, and deletefolder ). Before performing the preceding operations, you must first obtain the access handle (getfolder) of the folder ). The following example shows how to operate folders:

Private sub Command2-Click ()

Dim FSO as new FileSystemObject, FLDR as folder

Set FLDR = FSO. getfolder ("C :″)

'Get the access handle of the current folder

Debug. Print "parent folder name is:" & FLDR

'Print the folder name

Debug. Print "contained on drive" & FLDR. Drive 'print drive name

FSO. createfolder ("C:/Temp ″)

'Create a new folder with the FileSystemObject object

FSO. deletefolder ("C:/Temp ″)

'Delete the new folder

End sub

To access an object, you must first use the "get" method to obtain the access handle of the object. However, if you use the "Create" function to create a new object, the function returns a handle to the newly created object. In this case, you only need to set a variable to obtain the handle. You do not need to use the "get" method. For example, set FLDR = FSO. createfolder ("C:/temp2 ″).

4. File Operations

Read and Write files are implemented using open statements before VB6. FSO supports creating and reading and writing text files through textstream objects.

FSO creates an ordered text file using createtextfile, opentextfile, and openastextstream. After the file is created, you can write data in three steps:

1. Use the opentextfile method of the FileSystemObject object or the openastextstream method of the file object to open the text file for writing data.

2. Use the write, writeline, or writeblanklines methods of the textstream object to write data.

3. Use the close method of the textstream object to close the file.

The following example shows how to create a text file:

We can use the read, Readline, or readall methods of textstream objects to read data from a text file.

Private sub Command3-Click ()

Dim FSO as new FileSystemObject, Fil as file, TS as textstream

FSO. createtextfile "C:/testfile.txt ″

Set fil = FSO. GetFile ("C:/testfile.txt ″)

Set Ts = fil. openastextstream (forwriting)

TS. Write ("this is a test file! ″)

TS. Close

Set Ts = fil. openastextstream (forreading)

S = ts. Readline

Msgbox s

TS. Close

End sub

In addition to creating and reading and writing files, VB6 can also copy, move, and delete files using FileSystemObject objects, in the file object, the equivalent methods include copy, move, and delete. The routine is as follows:

Private sub Command4-Click ()

Dim FSO as new FileSystemObject, txtfile as textstream, fil1 as file, fil2 as file

Set txtfile = FSO. createtextfile ("C:/testfile.txt", true)

Txtfile. Write ("this is a test file! ″)

Txtfile. Close

Set fil1 = FSO. GetFile ("C:/testfile.txt ″)

Obtain a file handle

Fil1.copy ("C:/temp1/testfile.txt ″)

'Copy the file to the/temp1 directory

Fil1.move ("C:/temp2/testfile.txt ″)

'Move the file to the/temp2 directory

Set fil1 = FSO. GetFile ("C:/temp1/testfile.txt ″)

'Get the handle to the current location of these files

Set fil2 = FSO. GetFile ("C:/temp2/testfile.txt ″)

Fil1.delete

Fil2.delete

End sub

Posted by Sharky

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.