文章摘要
这篇文章介绍了两段VBA代码,用于展示文件系统驱动器类型和可用空间。第一段代码定义了`ShowDriveType`函数,用于根据指定路径返回驱动器的类型(如移动硬盘、固定磁盘、网络硬盘等)。第二段代码定义了`ShowFreeSpace`函数,用于根据指定路径返回驱动器的可用空间信息。文章还展示了如何调用这两个函数,并在消息框中显示结果。整体内容突出展示了如何通过脚本操作文件系统,适用于磁盘管理或故障排查场景。
Function ShowDriveType(drvpath)
Dim fso, d, t
Set fso=CreateObject(“Scripting.FileSystemObject”)
Set d=fso.GetDrive(fso.GetDriveName(drvpath))
Select Case d.DriveType
Case 0 t=”Unknown”
Case 1 t=”Removable” ‘移动硬盘
Case 2 t=”Fixed” ‘硬盘
Case 3 t=”Network” ‘网络硬盘
Case 4 t=”CD-ROM”
Case 5 t=”RAM Disk” ‘RAM
End Select
ShowDriveType=”Drive ” & d.DriveLetter & “: – ” & t
End Function
Function ShowDriveType(drvpath)
Dim fso, d, t
Set fso=CreateObject(“Scripting.FileSystemObject”)
Set d=fso.GetDrive(fso.GetDriveName(drvpath))
Select Case d.DriveType
Case 0 t=”Unknown”
Case 1 t=”Removable” ‘移动硬盘
Case 2 t=”Fixed” ‘硬盘
Case 3 t=”Network” ‘网络硬盘
Case 4 t=”CD-ROM”
Case 5 t=”RAM Disk” ‘RAM
End Select
ShowDriveType=”Drive ” & d.DriveLetter & “: – ” & t
End Function
Function ShowFreeSpace(drvPath)
Dim fso, d, s
Set fso=CreateObject(“Scripting.FileSystemObject”)
Set d=fso.GetDrive(fso.GetDriveName(drvPath)) ‘d为F:
s=”Drive ” & UCase(drvPath) & ” – ”
s=s & d.VolumeName & ” ”
s=s & “Free Space: ” & FormatNumber(d.FreeSpace/1024, 0)
s=s & ” KBytes”
ShowFreeSpace=s
End Function
Dim message
message=ShowDriveType(“F:\Programming\Applications”)
MsgBox message
message=ShowFreeSpace(“F:\Programming\Applications”)
MsgBox message
© 版权声明
文章版权归作者所有,未经允许请勿转载。


