Tài nguyên Ổ đĩa và Thư mục với FileSystemObject (FSO)

đăng 20:34 27 thg 3, 2012 bởi Hai Yen   [ đã cập nhật 07:42 16 thg 9, 2014 ]

With the FileSystemObject (FSO) object model, you can work with drives and folders programmatically just as you can in the Windows Explorer interactively. You can copy and move folders, get information about drives and folders, and perform other operations on drives and folders.

Getting Information About Drives

The Drive Object enables you to obtain information about the various drives attached to a system, either physically or over a network. The following table shows the information that you can obtain by using its properties.



The total size of the drive in bytes.

TotalSize Property

The space available on the drive in bytes.

AvailableSpace Property or FreeSpace Property

The letter assigned to the drive.

DriveLetter Property

The type of drive, for example, removable, fixed, network, CD-ROM, or RAM disk.

DriveType Property

The drive's serial number.

SerialNumber Property

The type of file system the drive uses, such as FAT, FAT32,and NTFS.

FileSystem Property

Whether a drive is available for use.

IsReady Property

The name of the share or volume.

ShareName Property or VolumeName Property

The path or root folder of the drive.

Path Property (FileSystemObject) orRootFolder Property

For more information, see FileSystemObject Sample Code.

Example of Using the Drive Object

Use the Drive object to gather information about a drive.

The following example demonstrates how to use the Drive object. In this example, the GetDrive Method method obtains a reference to an existing Drive object (in this case, drv).

Sub ShowDriveInfo(path)
Dim fso, drv, bytesPerGB, freeGB, totalGB, s

s = ""
bytesPerGB = 1024 * 1024 * 1024

Set fso = CreateObject("Scripting.FileSystemObject")
Set drv = fso.GetDrive(fso.GetDriveName(path))

s = s & drv.Path & " - "

if drv.IsReady Then
freeGB = drv.FreeSpace / bytesPerGB
totalGB = drv.TotalSize / bytesPerGB

s = s & FormatNumber(freeGB, 3) + " GB free of "
s = s & FormatNumber(totalGB, 3) + " GB"
s = s & "Not Ready"
End If
s = s & "<br />"

document.write (s)
End Sub
This language is not supported or no code example is available.
Working with Folders

Common folder tasks and the methods for performing them are described in the following table.



Create a folder.

CreateFolder Method

Delete a folder.

Delete Method or DeleteFolder Method

Move a folder.

Move Method or MoveFolder Method

Copy a folder.

Copy Method (FileSystemObject) or CopyFolder Method

Determine whether a folder exists on a drive.

FolderExists Method

Get an instance of an existing Folder object.

GetFolder Method

Determine the name of a folder's parent folder.

GetParentFolderName Method

Determine the path of system folders.

GetSpecialFolder Method

For more information, see FileSystemObject Sample Code.

The following example demonstrates how to use the Folder Object and FileSystemObject Object to work with folders and obtain information about them.

Sub ShowFolderInfo()
Dim fso, fldr, s, newLine, newFolderName
newLine = "<br />"

Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.GetFolder("c:\")

s = s & "Folder: " + fldr & newLine
s = s & "Drive: " + fldr.Drive & newLine

If fldr.IsRootFolder Then
s = s & "Root Folder"
s = s & "Parent Folder: " & fldr.ParentFolder
End If
s = s & newLine

' Create and delete a folder.
newFolderName = "C:\TempFolder1"
s = s & "Base Name of Added Folder: " & fso.GetBaseName(newFolderName)

s = s & newLine
End Sub