Understanding the File System Object FSO of VB

Source: Internet
Author: User

In VB programming, it is often necessary to deal with the file system, such as obtaining the remaining space of the hard disk, and determining whether a folder or file exists. Before VB releases a file system object, you need to call Windows API functions or use complicated processes to complete these functions, this makes programming complex, less reliable, and error-prone. Using the file system objects provided by Windows makes everything much easier. Below I will give you some examples that are commonly used in programming and provide them in the form of functions or procedures. Readers can directly use them in programming, or they can implement more powerful functions after improvement.
To apply the FSO object, you must reference a library named scripting by executing the "Project/reference" menu item of VB6.0 and adding the "Microsoft scripting RunTime" item in the reference list box. Then, we can see many objects, methods, and attributes in the scripting library in "Object Browser.
1. Determine the drive letter of the optical drive:
Function getcdrom () 'returns the drive letter (letter) of the optical drive)
Dim FSO as new FileSystemObject 'creates an instance of the FSO object
Dim fsodrive as drive, fsodrives as drives define the drive, drive set object
Set fsodrives = FSO. Drives
For each fsodrive in fsodrives 'traverse all available drives
If fsodrive. drivetype = CDROM then 'If the drive type is CDROM
Getcdrom = fsodrive. driveletter 'output its drive letter
Else
Getcdrom = ""
End if
Next
Set FSO = nothing
Set fsodrive = nothing
Set fsodrives = nothing
End Function
2. Determine whether a file or folder exists:
'Return Boolean value: True, false does not exist, filername file name
Function fileexist (filename as string)
Dim FSO as new FileSystemObject
If FSO. fileexists (filename) = true then
Fileexist = true
Else
Fileexist = false
End if
Set FSO = nothing
End Function
'Return Boolean value: True, false does not exist, Foldername folder
Function folderexist (Foldername as string)
Dim FSO as new FileSystemObject
If FSO. folderexists (Foldername) = true then
Folderexist = true
Else
Folderexist = false
End if
Set FSO = nothing
End Function
3. Get the drive parameters:
'Total disk space returned (unit: m), drive = drive letter a, c, d...
Function allspace (drive as string)
Dim FSO as new FileSystemObject, DRV as drive
Set DRV = FSO. getdrive (drive) 'To Get The DRV object instance.
If DRV. isready then' exists on the drive (disks exist in the drive or the Drive drive, and the hard disk access is normal)
Allspace = format (DRV. totalsize/(2 ^ 20), "0.00") 'converts bytes to megabytes
Else
Allspace = 0
End if
Set FSO = nothing
Set DRV = nothing
End Function
'Disk available space (unit: m), drive = drive letter a, c, d...
Function freespace (drive)
Dim FSO as new FileSystemObject, DRV as drive
Set DRV = FSO. getdrive (drive)
If DRV. isready then
Freespace = format (DRV. freespace/(2 ^ 20), "0.00 ")
End if
Set FSO = nothing
Set DRV = nothing
End Function
'Get the drive file system type, drive = drive letter a, c, d...
Function fstype (drive as string)
Dim FSO as new FileSystemObject, DRV as drive
Set DRV = FSO. getdrive (drive)
If DRV. isready then
Fstype = DRV. filesystem
Else
Fstype = ""
End if
Set FSO = nothing
Set DRV = nothing
End Function
4. Obtain the system folder path:
'Return to the Windows folder path
Function getwindir ()
Dim FSO as new FileSystemObject
Getwindir = FSO. getspecialfolder (windowsfolder)
Set FSO = nothing
End Function
'Return to the Windows/system folder path
Function getwinsysdir ()
Dim FSO as new FileSystemObject
Getwinsysdir = FSO. getspecialfolder (systemfolder)
Set FSO = nothing
End Function
5. Comprehensive application: a general process of file backup:
'Filename = file name, drive = drive, folder = folder (level 1)
Sub backupfile (filename as string, drive as string, folder as string)
Dim FSO as new FileSystemObject 'create a FSO object instance
Dim dest_path as string, counter as long
Counter = 0
Do While counter <6' continue detection if the drive is not ready. 6 seconds in total
Counter = counter + 1
Call waitfor (1) 'interval: 1 second
If FSO. Drives (drive). isready = true then
Exit do
End if
Loop
If FSO. Drives (drive). isready = false then' 6 seconds later, the target disk is not ready. Exit
Msgbox "target drive" & drive & "is not ready! ", Vbcritical
Exit sub
End if
If FSO. getdrive (drive). freespace <FSO. GetFile (filename). Size then
Msgbox "the target drive space is too small! ", Vbcritical 'the target drive has insufficient space and exits.
Exit sub
End if
If right (drive, 1) <> ":" Then
Drive = Drive &":"
End if
If left (folder, 1) <> "/" then
Folder = "/" & folder
End if
If right (folder, 1) <> "/" then
Folder = folder &"/"
End if
Dest_path = Drive & folder
If not FSO. folderexists (dest_path) then 'If the target Folder does not exist, create
FSO. createfolder dest_path
End if
FSO. copyfile filename, dest_path & FSO. getfilename (filename), true
'Copy, directly overwrite the file with the same name
Msgbox file backup is complete. ", Vbokonly
Set FSO = nothing
End sub
Private sub waitfor (delay as single) 'delay process. The unit of delay is about 1 second.
Dim starttime as single
Starttime = Timer
Do until (timer-starttime)> Delay
Loop
End sub
Zhang Qing Email: zhangking at 263.net QQ: 9365822
Http://www.why100000.com

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.