file.asp
Upload User: jinglin
Upload Date: 2021-12-20
Package Size: 545k
Code Size: 15k
Development Platform:

VBScript

  1. <% Option Explicit %>
  2. <html>
  3. <head>
  4. <title>filesystemobject sample(tsj搜集)</title>
  5. </head>
  6. <body>
  7. <%
  8. ' FileSystemObject 示例代码
  9. 'Copyright 1998  Microsoft Corporation。保留所有权利。 
  10. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  11. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  12. '
  13. ' 对于代码质量:
  14. '
  15. ' 1) 下面的代码有许多字符串操作,用"&"运算符来把短字符串连接在一起。由于
  16. '    字符串连接是费时的,所以这是一种低效率的写代码方法。无论如何,它是
  17. '    一种非常好维护的写代码方法,并且在这儿使用了这种方法,因为该程序执行
  18. '    大量的磁盘操作,而磁盘操作比连接字符串所需的内存操作要慢得多。
  19. '    记住这是示范代码,而不是产品代码。
  20. '
  21. ' 2) 使用了 "Option Explicit",因为访问声明过的变量,比访问未声明的变量要
  22. '    稍微快一些。它还能阻止在代码中发生错误,例如,把 DriveTypeCDROM 误拼
  23. '    成了 DriveTypeCDORM 。
  24. '
  25. ' 3) 为了使代码更可读,该代码中没有错误处理。虽然采取了防范措施,来保证代码
  26. '    在普通情况下没有错误,但文件系统是不可预知的。在产品代码中,使用
  27. '    On Error Resume Next 和 Err 对象来捕获可能发生的错误。
  28. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  29. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  30. '
  31. ' 一些容易取得的全局变量
  32. '
  33. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  34. Dim TabStop
  35. Dim NewLine
  36. Const TestDrive = "C"
  37. Const TestFilePath = "C:Test"
  38. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  39. '
  40. ' 由 Drive.DriveType 返回的常数
  41. '
  42. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  43. Const DriveTypeRemovable = 1
  44. Const DriveTypeFixed = 2
  45. Const DriveTypeNetwork = 3
  46. Const DriveTypeCDROM = 4
  47. Const DriveTypeRAMDisk = 5
  48. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  49. '
  50. ' 由 File.Attributes 返回的常数
  51. '
  52. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  53. Const FileAttrNormal  = 0
  54. Const FileAttrReadOnly = 1
  55. Const FileAttrHidden = 2
  56. Const FileAttrSystem = 4
  57. Const FileAttrVolume = 8
  58. Const FileAttrDirectory = 16
  59. Const FileAttrArchive = 32 
  60. Const FileAttrAlias = 64
  61. Const FileAttrCompressed = 128
  62. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  63. '
  64. ' 用来打开文件的常数
  65. '
  66. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  67. Const OpenFileForReading = 1 
  68. Const OpenFileForWriting = 2 
  69. Const OpenFileForAppending = 8 
  70. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  71. '
  72. ' ShowDriveType
  73. '
  74. ' 目的: 
  75. '
  76. ' 生成一个字符串,来描述给定 Drive 对象的驱动器类型。
  77. '
  78. ' 示范下面的内容
  79. '
  80. ' - Drive.DriveType
  81. '
  82. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  83. Function ShowDriveType(Drive)
  84. Dim S
  85.   
  86. Select Case Drive.DriveType
  87. Case DriveTypeRemovable
  88. S = "Removable"
  89. Case DriveTypeFixed
  90. S = "Fixed"
  91. Case DriveTypeNetwork
  92. S = "Network"
  93. Case DriveTypeCDROM
  94. S = "CD-ROM"
  95. Case DriveTypeRAMDisk
  96. S = "RAM Disk"
  97. Case Else
  98. S = "Unknown"
  99. End Select
  100. ShowDriveType = S
  101. End Function
  102. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  103. '
  104. ' ShowFileAttr
  105. '
  106. ' 目的: 
  107. '
  108. ' 生成一个字符串,来描述文件或文件夹的属性。
  109. '
  110. ' 示范下面的内容
  111. '
  112. ' - File.Attributes
  113. ' - Folder.Attributes
  114. '
  115. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  116. Function ShowFileAttr(File) ' File 可以是文件或文件夹
  117. Dim S
  118.    Dim Attr
  119. Attr = File.Attributes
  120. If Attr = 0 Then
  121. ShowFileAttr = "Normal"
  122. Exit Function
  123. End If
  124. If Attr And FileAttrDirectory  Then S = S & "Directory "
  125. If Attr And FileAttrReadOnly   Then S = S & "Read-Only "
  126. If Attr And FileAttrHidden     Then S = S & "Hidden "
  127. If Attr And FileAttrSystem     Then S = S & "System "
  128. If Attr And FileAttrVolume     Then S = S & "Volume "
  129. If Attr And FileAttrArchive    Then S = S & "Archive "
  130. If Attr And FileAttrAlias      Then S = S & "Alias "
  131. If Attr And FileAttrCompressed Then S = S & "Compressed "
  132. ShowFileAttr = S
  133. End Function
  134. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  135. '
  136. ' GenerateDriveInformation
  137. '
  138. ' 目的: 
  139. '
  140. ' 生成一个字符串,来描述可用驱动器的当前状态。
  141. '
  142. ' 示范下面的内容
  143. '
  144. ' - FileSystemObject.Drives 
  145. ' - Iterating the Drives collection
  146. ' - Drives.Count
  147. ' - Drive.AvailableSpace
  148. ' - Drive.DriveLetter
  149. ' - Drive.DriveType
  150. ' - Drive.FileSystem
  151. ' - Drive.FreeSpace
  152. ' - Drive.IsReady
  153. ' - Drive.Path
  154. ' - Drive.SerialNumber
  155. ' - Drive.ShareName
  156. ' - Drive.TotalSize
  157. ' - Drive.VolumeName
  158. '
  159. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  160. Function GenerateDriveInformation(FSO)
  161. Dim Drives
  162. Dim Drive
  163. Dim S
  164. Set Drives = FSO.Drives
  165. S = "Number of drives:" & TabStop & Drives.Count & NewLine & NewLine
  166. ' 构造报告的第一行。
  167. S = S & String(2, TabStop) & "Drive" 
  168. S = S & String(3, TabStop) & "File" 
  169. S = S & TabStop & "Total"
  170. S = S & TabStop & "Free"
  171. S = S & TabStop & "Available" 
  172. S = S & TabStop & "Serial" & NewLine
  173. ' 构造报告的第二行。
  174. S = S & "Letter"
  175. S = S & TabStop & "Path"
  176. S = S & TabStop & "Type"
  177. S = S & TabStop & "Ready?"
  178. S = S & TabStop & "Name"
  179. S = S & TabStop & "System"
  180. S = S & TabStop & "Space"
  181. S = S & TabStop & "Space"
  182. S = S & TabStop & "Space"
  183. S = S & TabStop & "Number" & NewLine
  184. ' 分隔行。
  185. S = S & String(105, "-") & NewLine
  186. For Each Drive In Drives
  187. S = S & Drive.DriveLetter
  188. S = S & TabStop & Drive.Path
  189. S = S & TabStop & ShowDriveType(Drive)
  190. S = S & TabStop & Drive.IsReady
  191. If Drive.IsReady Then
  192.      If DriveTypeNetwork = Drive.DriveType Then
  193. S = S & TabStop & Drive.ShareName 
  194. Else
  195. S = S & TabStop & Drive.VolumeName 
  196. End If    
  197. S = S & TabStop & Drive.FileSystem
  198. S = S & TabStop & Drive.TotalSize
  199. S = S & TabStop & Drive.FreeSpace
  200. S = S & TabStop & Drive.AvailableSpace
  201. S = S & TabStop & Hex(Drive.SerialNumber)
  202. End If
  203. S = S & NewLine
  204. Next  
  205. GenerateDriveInformation = S
  206. End Function
  207. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  208. '
  209. ' GenerateFileInformation
  210. '
  211. ' 目的: 
  212. '
  213. ' 生成一个字符串,来描述文件的当前状态。
  214. '
  215. ' 示范下面的内容
  216. '
  217. ' - File.Path
  218. ' - File.Name
  219. ' - File.Type
  220. ' - File.DateCreated
  221. ' - File.DateLastAccessed
  222. ' - File.DateLastModified
  223. ' - File.Size
  224. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  225. Function GenerateFileInformation(File)
  226. Dim S
  227. S = NewLine & "Path:" & TabStop & File.Path
  228. S = S & NewLine & "Name:" & TabStop & File.Name
  229. S = S & NewLine & "Type:" & TabStop & File.Type
  230. S = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(File)
  231. S = S & NewLine & "Created:" & TabStop & File.DateCreated
  232. S = S & NewLine & "Accessed:" & TabStop & File.DateLastAccessed
  233. S = S & NewLine & "Modified:" & TabStop & File.DateLastModified
  234. S = S & NewLine & "Size" & TabStop & File.Size & NewLine
  235. GenerateFileInformation = S
  236. End Function
  237. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  238. '
  239. ' GenerateFolderInformation
  240. '
  241. ' 目的: 
  242. '
  243. ' 生成一个字符串,来描述文件夹的当前状态。
  244. '
  245. ' 示范下面的内容
  246. '
  247. ' - Folder.Path
  248. ' - Folder.Name
  249. ' - Folder.DateCreated
  250. ' - Folder.DateLastAccessed
  251. ' - Folder.DateLastModified
  252. ' - Folder.Size
  253. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  254. Function GenerateFolderInformation(Folder)
  255. Dim S
  256. S = "Path:" & TabStop & Folder.Path
  257. S = S & NewLine & "Name:" & TabStop & Folder.Name
  258. S = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(Folder)
  259. S = S & NewLine & "Created:" & TabStop & Folder.DateCreated
  260. S = S & NewLine & "Accessed:" & TabStop & Folder.DateLastAccessed
  261. S = S & NewLine & "Modified:" & TabStop & Folder.DateLastModified
  262. S = S & NewLine & "Size:" & TabStop & Folder.Size & NewLine
  263. GenerateFolderInformation = S
  264. End Function
  265. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  266. '
  267. ' GenerateAllFolderInformation
  268. '
  269. ' 目的: 
  270. '
  271. ' 生成一个字符串,来描述一个文件夹和所有文件及子文件夹的当前状态。
  272. '
  273. ' 示范下面的内容
  274. '
  275. ' - Folder.Path
  276. ' - Folder.SubFolders
  277. ' - Folders.Count
  278. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  279. Function GenerateAllFolderInformation(Folder)
  280. Dim S
  281. Dim SubFolders
  282. Dim SubFolder
  283. Dim Files
  284. Dim File
  285. S = "Folder:" & TabStop & Folder.Path & NewLine & NewLine
  286. Set Files = Folder.Files
  287. If 1 = Files.Count Then
  288. S = S & "There is 1 file" & NewLine
  289. Else
  290. S = S & "There are " & Files.Count & " files" & NewLine
  291. End If
  292. If Files.Count <> 0 Then
  293. For Each File In Files
  294. S = S & GenerateFileInformation(File)
  295. Next
  296. End If
  297. Set SubFolders = Folder.SubFolders
  298. If 1 = SubFolders.Count Then
  299. S = S & NewLine & "There is 1 sub folder" & NewLine & NewLine
  300. Else
  301. S = S & NewLine & "There are " & SubFolders.Count & " sub folders" & NewLine & NewLine
  302. End If
  303. If SubFolders.Count <> 0 Then
  304. For Each SubFolder In SubFolders
  305. S = S & GenerateFolderInformation(SubFolder)
  306. Next
  307. S = S & NewLine
  308. For Each SubFolder In SubFolders
  309. S = S & GenerateAllFolderInformation(SubFolder)
  310. Next
  311. End If
  312. GenerateAllFolderInformation = S
  313. End Function
  314. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  315. '
  316. ' GenerateTestInformation
  317. '
  318. ' 目的: 
  319. '
  320. ' 生成一个字符串,来描述 C:Test 文件夹和所有文件及子文件夹的当前状态。
  321. '
  322. ' 示范下面的内容
  323. '
  324. ' - FileSystemObject.DriveExists
  325. ' - FileSystemObject.FolderExists
  326. ' - FileSystemObject.GetFolder
  327. '
  328. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  329. Function GenerateTestInformation(FSO)
  330. Dim TestFolder
  331. Dim S
  332. If Not FSO.DriveExists(TestDrive) Then Exit Function
  333. If Not FSO.FolderExists(TestFilePath) Then Exit Function
  334. Set TestFolder = FSO.GetFolder(TestFilePath)
  335. GenerateTestInformation = GenerateAllFolderInformation(TestFolder) 
  336. End Function
  337. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  338. '
  339. ' DeleteTestDirectory
  340. '
  341. ' 目的: 
  342. '
  343. ' 清理 test 目录。
  344. '
  345. ' 示范下面的内容
  346. '
  347. ' - FileSystemObject.GetFolder
  348. ' - FileSystemObject.DeleteFile
  349. ' - FileSystemObject.DeleteFolder
  350. ' - Folder.Delete
  351. ' - File.Delete
  352. '
  353. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  354. Sub DeleteTestDirectory(FSO)
  355. Dim TestFolder
  356. Dim SubFolder
  357. Dim File
  358. ' 有两种方法可用来删除文件:
  359. FSO.DeleteFile(TestFilePath & "BeatlesOctopusGarden.txt")
  360. Set File = FSO.GetFile(TestFilePath & "BeatlesBathroomWindow.txt")
  361. File.Delete
  362. ' 有两种方法可用来删除文件夹:
  363. FSO.DeleteFolder(TestFilePath & "Beatles")
  364. FSO.DeleteFile(TestFilePath & "ReadMe.txt")
  365. Set TestFolder = FSO.GetFolder(TestFilePath)
  366. TestFolder.Delete
  367. End Sub
  368. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  369. '
  370. ' CreateLyrics
  371. '
  372. ' 目的: 
  373. '
  374. ' 在文件夹中创建两个文本文件。
  375. '
  376. '
  377. ' 示范下面的内容
  378. '
  379. ' - FileSystemObject.CreateTextFile
  380. ' - TextStream.WriteLine
  381. ' - TextStream.Write
  382. ' - TextStream.WriteBlankLines
  383. ' - TextStream.Close
  384. '
  385. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  386. Sub CreateLyrics(Folder)
  387. Dim TextStream
  388. Set TextStream = Folder.CreateTextFile("OctopusGarden.txt")
  389. TextStream.Write("Octopus' Garden ") ' 请注意,该语句不添加换行到文件中。
  390. TextStream.WriteLine("(by Ringo Starr)")
  391. TextStream.WriteBlankLines(1)
  392. TextStream.WriteLine("I'd like to be under the sea in an octopus' garden in the shade,")
  393. TextStream.WriteLine("He'd let us in, knows where we've been -- in his octopus' garden in the shade.")
  394. TextStream.WriteBlankLines(2)
  395. TextStream.Close
  396. Set TextStream = Folder.CreateTextFile("BathroomWindow.txt")
  397. TextStream.WriteLine("She Came In Through The Bathroom Window (by Lennon/McCartney)")
  398. TextStream.WriteLine("")
  399. TextStream.WriteLine("She came in through the bathroom window protected by a silver spoon")
  400. TextStream.WriteLine("But now she sucks her thumb and wanders by the banks of her own lagoon")
  401. TextStream.WriteBlankLines(2)
  402. TextStream.Close
  403. End Sub
  404. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  405. '
  406. ' GetLyrics
  407. '
  408. ' 目的: 
  409. '
  410. ' 显示 lyrics 文件的内容。
  411. '
  412. '
  413. ' 示范下面的内容
  414. '
  415. ' - FileSystemObject.OpenTextFile
  416. ' - FileSystemObject.GetFile
  417. ' - TextStream.ReadAll
  418. ' - TextStream.Close
  419. ' - File.OpenAsTextStream
  420. ' - TextStream.AtEndOfStream
  421. ' - TextStream.ReadLine
  422. '
  423. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  424. Function GetLyrics(FSO)
  425. Dim TextStream
  426. Dim S
  427. Dim File
  428. ' 有多种方法可用来打开一个文本文件,和多种方法来从文件读取数据。
  429. ' 这儿用了两种方法来打开文件和读取文件:
  430. Set TextStream = FSO.OpenTextFile(TestFilePath & "BeatlesOctopusGarden.txt", OpenFileForReading)
  431. S = TextStream.ReadAll & NewLine & NewLine
  432. TextStream.Close
  433. Set File = FSO.GetFile(TestFilePath & "BeatlesBathroomWindow.txt")
  434. Set TextStream = File.OpenAsTextStream(OpenFileForReading)
  435. Do  While Not TextStream.AtEndOfStream
  436. S = S & TextStream.ReadLine & NewLine
  437. Loop
  438. TextStream.Close
  439. GetLyrics = S
  440. End Function
  441. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  442. '
  443. ' BuildTestDirectory
  444. '
  445. ' 目的: 
  446. '
  447. ' 创建一个目录分层结构来示范 FileSystemObject。
  448. '
  449. ' 以这样的次序来创建分层结构:
  450. '
  451. ' C:Test
  452. ' C:TestReadMe.txt
  453. ' C:TestBeatles
  454. ' C:TestBeatlesOctopusGarden.txt
  455. ' C:TestBeatlesBathroomWindow.txt
  456. '
  457. '
  458. ' 示范下面的内容
  459. '
  460. ' - FileSystemObject.DriveExists
  461. ' - FileSystemObject.FolderExists
  462. ' - FileSystemObject.CreateFolder
  463. ' - FileSystemObject.CreateTextFile
  464. ' - Folders.Add
  465. ' - Folder.CreateTextFile
  466. ' - TextStream.WriteLine
  467. ' - TextStream.Close
  468. '
  469. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  470. Function BuildTestDirectory(FSO)
  471. Dim TestFolder
  472. Dim SubFolders
  473. Dim SubFolder
  474. Dim TextStream
  475. ' 排除(a)驱动器不存在,或(b)要创建的目录已经存在的情况。
  476. If Not FSO.DriveExists(TestDrive) Then
  477. BuildTestDirectory = False
  478. Exit Function
  479. End If
  480. If FSO.FolderExists(TestFilePath) Then
  481. BuildTestDirectory = False
  482. Exit Function
  483. End If
  484. Set TestFolder = FSO.CreateFolder(TestFilePath)
  485. Set TextStream = FSO.CreateTextFile(TestFilePath & "ReadMe.txt")
  486. TextStream.WriteLine("My song lyrics collection")
  487. TextStream.Close
  488. Set SubFolders = TestFolder.SubFolders
  489. Set SubFolder = SubFolders.Add("Beatles")
  490. CreateLyrics SubFolder
  491. BuildTestDirectory = True
  492. End Function
  493. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  494. '
  495. ' 主程序
  496. '
  497. ' 首先,它创建一个 test 目录,以及一些子文件夹和文件。 
  498. ' 然后,它转储有关可用磁盘驱动器和 test 目录的某些信息,
  499. ' 最后,清除 test 目录及其所有内容。
  500. '
  501. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  502. Sub Main
  503. Dim FSO
  504. ' 设立全局变量。
  505. TabStop = Chr(9)
  506. NewLine = Chr(10)
  507. Set FSO = CreateObject("Scripting.FileSystemObject")
  508. If Not BuildTestDirectory(FSO) Then 
  509. Print "Test directory already exists or cannot be created.  Cannot continue."
  510. Exit Sub
  511. End If
  512. Print GenerateDriveInformation(FSO) & NewLine & NewLine
  513. Print GenerateTestInformation(FSO) & NewLine & NewLine
  514. Print GetLyrics(FSO) & NewLine & NewLine
  515. 'DeleteTestDirectory(FSO)
  516. End Sub
  517. sub print(x)
  518.     response.write "<pre><font face='宋体',size=1>"
  519.     response.write x
  520.     response.write "</font></pre>"
  521. end sub
  522. main
  523. %>
  524. </body>
  525. <html>