VBScript FileSystem 文件系统对象
顾名思义,FSO 对象可帮助开发人员使用驱动器、文件夹和文件。 在本节中,我们将讨论 −
对象和集合
Sr.No. | 对象类型和描述 |
---|---|
1 | Drive Drive 是一个对象。 包含允许您收集有关连接到系统的驱动器的信息的方法和属性 |
2 | Drives Drives 是一个集合。 它提供物理或逻辑连接到系统的驱动器列表。 |
3 | File File 是一个对象。 它包含允许开发人员创建、删除或移动文件的方法和属性。 |
4 | Files Files 是一个集合。 它提供文件夹中包含的所有文件的列表。 |
5 | Folder Folder 是一个对象。 它提供了允许开发人员创建、删除或移动文件夹的方法和属性。 |
6 | Folders Folders 是一个集合。 它提供文件夹中所有文件夹的列表。 |
7 | TextStream TextStream 是一个对象。 它使开发人员能够读取和写入文本文件。 |
Drive
Drive 是一个对象,它提供对特定磁盘驱动器或网络共享的属性的访问。 Drive 对象支持以下属性 −
- AvailableSpace
- DriveLetter
- DriveType
- FileSystem
- FreeSpace
- IsReady
- Path
- RootFolder
- SerialNumber
- ShareName
- TotalSize
- VolumeName
示例
<!DOCTYPE html> <html> <body> <script language = "vbscript" type = "text/vbscript"> Dim oFS, drive, space Set oFS = CreateObject("Scripting.FileSystemObject") Set drive = oFS.GetDrive(oFS.GetDriveName("C:\")) space = "Drive " & UCase(drvPath) & " - " space = space & drive.VolumeName & " " space = space & "Free Space: " & FormatNumber(drive.FreeSpace/1024, 0) space = space & " Kbytes" Document.write space </script> </body> </html>
如果上面的脚本保存为 HTML 并在 IE 中执行,我们将在控制台中得到以下输出。
Drive - Win 7 Free Space:20,154,059 Kbytes
Drives
Drives是一个集合,它提供了连接到系统的所有物理或逻辑驱动器的详细信息。 它有两个属性 −
- Count 属性
- Item 属性
示例
<!DOCTYPE html> <html> <body> <script language = "vbscript" type = "text/vbscript"> Dim oFS, d, dc, s, n Set oFS = CreateObject("Scripting.FileSystemObject") Set dc = oFS.Drives For Each d in dc n = "" s = s & d.DriveLetter & " - " If d.DriveType = 3 Then n = d.ShareName ElseIf d.IsReady Then n = d.VolumeName Else n = "Drive not available" End If s = s & n & "::" Next document.write s document.write dc.count </script> </body> </html>
如果上面的脚本保存为 HTML 并在 IE 中执行,我们将在控制台中得到以下输出。
C- Win 7::D-Personal ::E-Common::F-Songs:: 4
File
File 是一个对象,它包含允许开发人员创建、删除或移动文件的属性和方法。
方法
- Copy
- Delete
- Move
- openasTextStream
属性
- Attributes
- DateCreated
- DateLastAccessed
- DateLastModified
- Drive
- Name
- ParentFolder
- Path
- ShortName
- ShortPath
- Size
- Type
示例
<!DOCTYPE html> <html> <body> <script language = "vbscript" type = "text/vbscript"> Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFile("C:\user.js") document.write "Line 1: "& f.DateCreated & "<br />" document.write "Line 2: "& f.Attributes & "<br />" document.write "Line 3: "& f.DateLastAccessed & "<br />" document.write "Line 4: "& f.DateLastModified & "<br />" document.write "Line 5: "& f.Drive & "<br />" document.write "Line 6: "& f.Name & "<br />" document.write "Line 7: "& f.ParentFolder & "<br />" document.write "Line 8: "& f.Path & "<br />" document.write "Line 9: "& f.ShortName & "<br />" document.write "Line 10: "& f.ShortPath & "<br />" document.write "Line 11: "& f.Size & "<br />" document.write "Line 12: "& f.Type & "<br />" f.copy ("D:\") & "<br />" 'copying to file to another location' f.Move ("E:\") & "<br />" 'Move the file to another location' f.Delete ("D:\") & "<br />" 'Delete to file from one location' </script> </body> </html>
如果上面的脚本保存为 HTML 并在 IE 中执行,我们将在控制台中得到以下输出。
Line 1: 08/02/13 06:57:34 Line 2: 32 Line 3: 08/02/13 06:57:34 Line 4: 04/18/12 22:23:37 Line 5: C: Line 6: user.js Line 7: C:\ Line 8: C:\user.js Line 9: user.js Line 10: C:\user.js Line 11: 474 Line 12: JScript Script File
Files
Files 是一个集合,它提供文件夹中包含的所有文件的列表。
属性
- Count
- Item
示例
<!DOCTYPE html> <html> <body> <script language = "vbscript" type = "text/vbscript"> Dim fso, f, f1, fc, s Set oFS = CreateObject("Scripting.FileSystemObject") 'get the folder by giving its path Set f = oFS.GetFolder("D:\PROJECT\") Set fc = f.Files 'Get Item Set s = fc.Item("sendmail.vbs") 'Get Count x = fc.Count Document.write s Document.write x </script> </body> </html>
如果上面的脚本保存为 HTML 并在 IE 中执行,我们将在控制台中得到以下输出。
D:\PROJECT\sendmail.vbs 6
Folder
Folder是一个对象,它包含允许开发人员创建、删除或移动文件夹的属性和方法。
方法
- Copy
- Delete
- Move
- CreateTextFile
属性
- Attributes
- DateCreated
- DateLastAccessed
- DateLastModified
- Drive
- Files
- IsRootFolder
- Name
- ParentFolder
- Path
- ShortName
- ShortPath
- Size
- SubFolders
- Type
示例
<!DOCTYPE html> <html> <body> <script language = "vbscript" type = "text/vbscript"> Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") ' Enter a Folder Name that exists on your System' Set f = fso.GetFolder("D:\PROJECT\") ShowFileInfo = "Created: " & f.DateCreated & "<br / >" document.write ShowFileInfo ShowFileInfo = "attributes " & f.attributes & "<br / >" document.write ShowFileInfo ShowFileInfo = "Last Accessed : " & f.DateLastAccessed & "<br / >" document.write ShowFileInfo ShowFileInfo = "DateLastModified : " & f.DateLastModified & "<br / >" document.write ShowFileInfo ShowFileInfo = "Drive : " & f.Drive & "<br / >" document.write ShowFileInfo ShowFileInfo = "count : " & f.Files.count & "<br / >" document.write ShowFileInfo ShowFileInfo = "IsRoot folder : " &f.IsRootFolder & "<br / >" document.write ShowFileInfo ShowFileInfo = "Name : " & f.Name & "<br / >" document.write ShowFileInfo ShowFileInfo = "parent folder : " & f.ParentFolder & "<br / >" document.write ShowFileInfo ShowFileInfo = "Path : " & f.Path & "<br / >" document.write ShowFileInfo ShowFileInfo = "shortname : " & f.ShortName & "<br / >" document.write ShowFileInfo ShowFileInfo = "ShortPath : " & f.ShortPath & "<br / >" document.write ShowFileInfo ShowFileInfo = "File Size : " & f.Size & "<br / >" document.write ShowFileInfo ShowFileInfo = "Type : " & f.Type & "<br / >" document.write ShowFileInfo </script> </body> </html>
如果上面的脚本保存为 HTML 并在 IE 中执行,我们将在控制台中得到以下输出。
Created: 22/02/2012 8:24:57 PM attributes 16 Last Accessed : 1/08/2013 12:48:36 PM DateLastModified : 1/08/2013 12:48:36 PM Drive : D: count : 6 IsRoot folder : False Name : PROJECT parent folder : D:\ Path : D:\PROJECT shortname : PROJECT ShortPath : D:\PROJECT File Size : 8655239975 Type : File folder
Folders
Folders 是文件夹对象中所有文件夹对象的集合。
方法
- Add
属性
- Count
- Item
示例
如果上述脚本保存为 HTML 并在 IE 中执行,我们将创建一个名为"Test_Folder"的文件夹。
<!DOCTYPE html> <html> <body> <script language = "vbscript" type = "text/vbscript"> Dim fso, f, fc, nf Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFolder("D:\PROJECT") Set fc = f.SubFolders folderName = "Test_Folder" If folderName <> "" Then Set nf = fc.Add(folderName) Else Set nf = fc.Add("New Folder") End If </script> </body> </html>
TextStream
TextStream 对象帮助开发人员无缝地处理文本文件。 开发人员可以使用文本流对象读取、写入或附加内容到文本文件。
语法
TextStream.{property | method( )}
示例
如果上述脚本保存为 HTML 并在 IE 中执行,我们将创建一个名为"Test_Folder"的文件夹。
<!DOCTYPE html> <html> <body> <script language = "vbscript" type = "text/vbscript"> Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") Dim objTextFile Set objTextFile = objFSO.CreateTextFile("D:\Testfile.txt") objTextFile.Close Const ForAppending = 8 Set objTextFile = objFSO.OpenTextFile("D:\Testfile.txt",ForAppending,True) objTextFile.WriteLine "Welcome to VBScript Programming" objTextFile.Close Set objTextFile = Nothing Set objFSO = Nothing </script> </body> </html>
如果上述脚本保存为 HTML 并在 IE 中执行,它将在 D:\ 中创建一个文本文件,并附加在 WriteLine 方法中指定的字符串。
Welcome to VBScript Programming