明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 938|回复: 3

分享源代码:文件对象模块

[复制链接]
发表于 2018-12-6 19:50 | 显示全部楼层 |阅读模式
       FileSystemObject 对象的作用:提供对计算机文件系统的访问,它允许我们在代码内操作文本文件、文件夹及驱动器。FileSystemObject 对象提供一个属性和一系列方法,可用它们来操纵 FileSystemObject 对象实现的一些从属对象。我把常用功能写成了模块,供大家使用,代码如下:

  1. Public Function FolderExists(FolderPath As String) As Boolean
  2. '检查目录是否存在
  3. Dim fso As Object
  4. Set fso = CreateObject("Scripting.filesystemobject")
  5. FolderExists = fso.FolderExists(FolderPath)
  6. Set fso = Nothing
  7. End Function
  8. Public Function FileExists(FilePath As String) As Boolean
  9. '检查文件是否存在
  10. Dim fso As Object
  11. Set fso = CreateObject("Scripting.filesystemobject")
  12. FileExists = fso.FileExists(FilePath)
  13. Set fso = Nothing
  14. End Function

  15. Public Function GetDriveName(FilePath As String) As String
  16. '从路径中提取驱动器名(FilePath=“C:\WINDOWS\Test.TXT”,返回 C:)
  17. Dim fso As Object
  18. Set fso = CreateObject("Scripting.filesystemobject")
  19. GetDriveName = fso.GetDriveName(FilePath)
  20. Set fso = Nothing
  21. End Function
  22. Public Function GetFolderName(FilePath As String) As String
  23. '从路径中提取目录名(FilePath=“C:\WINDOWS\Test.TXT”,返回 C:\WINDOWS)
  24. Dim fso As Object
  25. Set fso = CreateObject("Scripting.filesystemobject")
  26. GetFolderName = fso.GetParentFolderName(FilePath)
  27. Set fso = Nothing
  28. End Function
  29. Public Function GetFileName(FilePath As String) As String
  30. '从路径中提取文件名(FilePath=“C:\WINDOWS\Test.TXT”,返回 Test.TXT)
  31. Dim fso As Object
  32. Set fso = CreateObject("Scripting.filesystemobject")
  33. GetFileName = fso.GetFileName(FilePath)
  34. Set fso = Nothing
  35. End Function
  36. Public Function GetExtensionName(FilePath As String) As String
  37. '从路径中提取文件扩展名(FilePath=“C:\WINDOWS\Test.TXT”,返回 TXT)
  38. Dim fso As Object
  39. Set fso = CreateObject("Scripting.filesystemobject")
  40. GetExtensionName = fso.GetExtensionName(FilePath)
  41. Set fso = Nothing
  42. End Function

  43. Public Function GetBaseName(FilePath As String) As String
  44. '从路径中提取文件名(不带路径与扩展名)(FilePath=“C:\WINDOWS\Test.TXT”,返回 Test)
  45. Dim fso As Object
  46. Set fso = CreateObject("Scripting.filesystemobject")
  47. GetBaseName = fso.GetBaseName(FilePath)
  48. Set fso = Nothing
  49. End Function

  50. Public Function GetFileLastDate(FilePath As String) As Date
  51. '返回文件最后修改日期(FilePath=“C:\WINDOWS\Test.TXT”,返回时间如: 2000-1-1 22:22:22)
  52. Dim fso As Object
  53. Dim objFile As Object
  54. Set fso = CreateObject("Scripting.filesystemobject")
  55. Set objFile = fso.GetFile(FilePath)
  56. GetFileLastDate = objFile.DateLastModified
  57. Set fso = Nothing
  58. End Function
  59. Public Function GetFileSize(FilePath As String) As String
  60. '返回文件大小(FilePath=“C:\WINDOWS\Test.TXT”,返回如: 1kb)
  61. Dim fso As Object
  62. Dim objFile As Object
  63. Set fso = CreateObject("Scripting.filesystemobject")
  64. Set objFile = fso.GetFile(FilePath)
  65. GetFileSize = FormatNumber(objFile.Size / 1024, 0) & "KB"
  66. Set fso = Nothing
  67. End Function
  68. Public Function GetShortName(FilePath As String) As String
  69. '从路径中提取8.3短文件名(FilePath=“C:\WINDOWS\Test.TXT”,返回 Test.TXT)
  70. Dim fso As Object
  71. Dim objFile As Object
  72. Set fso = CreateObject("Scripting.filesystemobject")
  73. Set objFile = fso.GetFile(FilePath)
  74. GetShortName = objFile.ShortName
  75. Set fso = Nothing
  76. End Function
  77. Public Function GetShortPath(FilePath As String) As String
  78. '从路径中提取8.3短文件路径(FilePath=“C:\WINDOWS\Test.TXT”,返回 C:\WINDOWS\Test.TXT)
  79. Dim fso As Object
  80. Dim objFile As Object
  81. Set fso = CreateObject("Scripting.filesystemobject")
  82. Set objFile = fso.GetFile(FilePath)
  83. GetShortPath = objFile.ShortPath
  84. Set fso = Nothing
  85. End Function

  86. Public Function GetFileList(Folder As String) As String()
  87. '列出目录中文件,以数组形式返回目录下文件(不含子目录),下标为0
  88. Dim fso As Object
  89. Dim objFolder As Object
  90. Dim objFile As Object
  91. Dim arrTmp() As String
  92. Dim i As Long
  93. Set fso = CreateObject("Scripting.filesystemobject")
  94. Set objFolder = fso.GetFolder(Folder) '获得目录中的所有对象

  95. ReDim arrTmp(objFolder.Files.Count)
  96. For Each objFile In objFolder.Files                     '遍历文件夹下的文件
  97.     arrTmp(i) = objFile.Path
  98.     i = i + 1
  99. Next
  100. GetFileList = arrTmp
  101. Set fso = Nothing
  102. End Function



  103. Public Function GetDriveFreeSpace(FilePath As String) As String
  104. Dim fso As Object
  105. Dim Drive As Object
  106. Set fso = CreateObject("Scripting.filesystemobject")
  107. Set Drive = fso.GetDrive(GetDriveName(FilePath))
  108. GetDriveFreeSpace = FormatNumber(Drive.FreeSpace / 1024 / 1024 / 1024, 0) & "GB"
  109. Set fso = Nothing
  110. End Function

  111. Public Function GetDriveTotalSize(FilePath As String) As String
  112. Dim fso As Object
  113. Dim Drive As Object
  114. Set fso = CreateObject("Scripting.filesystemobject")
  115. Set Drive = fso.GetDrive(GetDriveName(FilePath))
  116. GetDriveTotalSize = FormatNumber(Drive.TotalSize / 1024 / 1024 / 1024, 0) & "GB"
  117. Set fso = Nothing
  118. End Function

  119. Public Function GetDriveSerialNumber(FilePath As String) As String
  120. Dim fso As Object
  121. Dim Drive As Object
  122. Set fso = CreateObject("Scripting.filesystemobject")
  123. Set Drive = fso.GetDrive(GetDriveName(FilePath))
  124. GetDriveSerialNumber = Drive.SerialNumber
  125. Set fso = Nothing
  126. End Function
  127. Public Function GetDriveList() As String()
  128. '获得系统所有盘符
  129. Dim fso As Object
  130. Dim objDrive As Object
  131. Dim arrTmp() As String
  132. Dim i As Long
  133. Set fso = CreateObject("Scripting.filesystemobject")
  134. ReDim arrTmp(fso.Drives.Count)
  135. For Each objDrive In fso.Drives                     '遍历
  136. '    Debug.Print objDrive.DriveLetter, objDrive.DriveType
  137.     arrTmp(i) = objDrive.DriveLetter
  138.     i = i + 1
  139. Next
  140. GetDriveList = arrTmp
  141. Set fso = Nothing
  142. End Function


评分

参与人数 2明经币 +1 金钱 +5 收起 理由
水吉空 + 5 很给力!
BaoWSE + 1 赞一个!

查看全部评分

发表于 2018-12-7 12:34 | 显示全部楼层
谢谢楼主分享经验!
发表于 2019-4-24 08:27 | 显示全部楼层
感谢楼主分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-23 19:17 , Processed in 0.891338 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表