Use the VB operating directory and folders

xiaoxiao2021-03-06  38

Windows also provides us with a FileSystemObject (referred to as fso) object. All objects of computer file systems are included in the FSO object model. See the table below. Use these objects that can be more convenient to operate file systems.

Object

Features

Drive allows the system's drive information, such as the available space Folder, such as the drive, allows you to create, delete, or moving folders, and to create, delete, or moving files to the name, path, etc. of the system query folder, and query files to the system Name, path, etc. FileSysterObject This is a primary object, providing a complete set of methods for creating, deleting, collecting information, and usual operating drives, folders, and files. TextStream allows reading and writing text files

Let's take a look at how to use the FSO object to display, new, modify, and delete the directory.

The FSO object model is included in the Scripting Type library, this type of inventory is in the Scrrun.dll file. Use the FSO object model, first create a FileSystemObject object. There are two ways to be realized. One is to select the "Microsoft Scripting Runtime" item from the "Project" menu, then declare a variable of a FileSystemObject type in the code window. The statement is as follows:

DIM FSO AS New FileSystemObject

Another method is to create a FileSystemObject object using the CreatObject method in the code. The statement is as follows:

DIM FSO As Object 'declares target variable set fso = creatobject ("scripting. FilesystemObject")' Create an FSO object

We look at the main properties of FileSystemObject.

1, drive

(1) DRIVES attribute is the unique property of the FileSystemObject object, which returns a read-only collection of all available drives in the DRIVES collection. For delete drivers, you don't need to insert the media into it, you can display it in the DRIVES collection. Below is its main attributes, there are two: one is count, the other is the item.count property Returns the number of entries in the DRIVES collection or Dictionary object .Item property is used to return or set up the DRIVES collection or Dictionary objects related to the specified keyword s project.

The following code illustrates how to get a DRIVES collection, and how to use for EACN ... NEXT statements to access each Drive in the collection:

Sub showdrivelist () DIM FS AS Object, D, DC, S, N Create File System Object SET FS = CreatObject ("Scripting. FileSystemObject") Create Drive Collection SET DC = fs.drives' Drive Object for Each D in DC S = S & D.Driveletter & "-" Formatted text if D.Drivertype = Remote Then ' Route End IF S = S & N & VBCRLF 'Formatted Text NEXT MSGBOX S' Display Text End Sub

(2) Of course, we can also use the Drive object .Drive objects provide access methods for disk drives or network sharing properties. Below is its main attribute and its explanation:

Are Availablespace drive is specified with the space DriveLetter drive letter Freespace drive free space DriverType drive type TotalSize drive all the space FileSystem drive file system IsReady drive is ready Path drive method name the main root directory SerizlNumber drive serial number VolumeName drive label ShareName drive share is GetDrive, this method uses to access an existing drive, which returns a Drive object corresponding to the drive in the specified path. In the following code, we will explain how to get a specified drive:

Sub ShowFreespace (drvpath) 'Displays the information of the drive in the specified directory DIM FS AS Object, D, S set fs = createObject ("scripting.filesystemObject")' Create a file system object set d = fs.getdrive (fs.getdrivename (drvpath) )) 'Create and get the drive S = "Drive" & ucase (drvpath) & "-" formatted text s = s & d.volumename & vbcrlf' Get the drive's volume S = S = S & " FreeSpace: "& FormatNumber (DREESPACE / 1024, 0) 'Calculate the remaining disk space of the drive S = S &" Kbytes "MSGBox S' Display End Sub

Here are other methods of FileSystemObject

CREATEFOLDER This method is to create a folder. The folder to be created must not exist, otherwise an error. CreateTextFile The function of this method is to generate a specified file name and return a TextStream object that can be used to read and write the specified file. If the OverWrite parameter is false or not specified, an error will result in an existing file. Deletefile The role of this method is to delete a specified file. If the specified file does not exist, an error is wrong. DeleteFolder The role of this method is to delete a folder and its content. If you do not find a matching folder, an error is wrong. This method cannot determine if content is included in the folder. DriveExists The role of this method is to determine if the drive is present. Returns true if the specified drive exists, otherwise returns false. But for the driver that can delete media, even if there is no medium exists, the DriveExists method returns TRUE, so it is best to use the IsReady property to determine if the drive is ready. FileExists' role is to determine if the specified file object exists in the current folder FOLDEREXISTS. The function of this method is to determine if the specified folder object is present in the current folder GetDrive. The role of this method is to return one in the specified path. Drive objects corresponding to the drive. For the network drive, the shared is exist first. GerdriveName The role of this method is to return a string that includes a drive name on a specified path. If the drive cannot be determined, a 0 length string is returned. This method only works on the specified path, which does not try to parse the path, nor does it check if the specified path is present. GetExtensionName The role of this method is to return the extension of the last component in the specified path. GetFile The role of this method is to return the File object related to a file in the specified path. Be sure to ensure that the specified file is actually existing. Otherwise an error will occur. GetFileName The role of this method is to return the file name of the last component of the specified path. GetFolder The role of this method is to return the Folder object related to a folder on the specified path. To ensure that the specified folder is actually existing, otherwise it will be wrong. The first part of using the FileSystemObjectD is the getFolder method with FileSystemObjectd. Get the Folder object getParentFoldername This method is to return a name of the parent folder that contains the last component of the specified path. The role of the MoveFile is to move one or more files from one place to another. The role of this method is to move one or more folders, if the source path contains a wildcard, or the destination path is ended in the slash (/), indicating that the destination path is the existing path, and moves in this folder Folder. Otherwise, the dedicated path is a name for the target folder to create. If the destination path is an existing file or destination path as a directory, it is wrong. If no file matches wildcards in the source path Also an error. OpenTextFile This method can be used to open a specified file and return a TextStream object. Used to read or add files.