VBS: FSO对象及文件读写

1. FSO概述

文件系统是所有操作系统最重要的部分之一,脚本经常会需要对文件及文件夹进行访问和管理,在Vbs 中对桌面和文件系统进行访问的顶级对象是FileSystemObject(FSO),这个对象特别复杂,是vbs 进行文件操作的核心。

FileSytemObject是一种主控对象,通过它可以访问一系列的对象。FileSystemObject中所有对象一起提供了访问和操作Windows文件系统的功能。

FSO对象模型主要由以下的对象和集合组成的。

FileSystemObject

2. FSO 操作模型

现在我们遇到如下的一些问题:

  • 如何通过FSO来查询文件和文件夹
  • 如何通过FSO来创建Text文件并且对文件进行各种操作
  • 如何来查询各种驱动器的信息

我们可以通过如下的一个模型图,来很快的做到这些事情:

FSO Model

1) 驱动器的操作

获取某一个驱动器的信息,如它的大小,名字,文件系统 等等,在这里我们主要使用驱动器对象,该对象没有方法,只有属性。

它的主要属性主要如下图所示:

Drive 对象

用一段代码,来演示一下如何对Drive对象和Drives集合进行操作。

  • 这段代码的主要作用是看看C驱动器的相关信息

Option Explicit
Dim objFSO, objDrive
Dim strDrive
Dim strTotalsize,strVolumeName
Dim strDriveInformation
strDrive = "C:"
'call FileSystemObject Model
Set objFSO = CreateObject("scripting.filesystemobject")
'Get the Drive Name
Set objDrive = objFSO.GetDrive(objFSO.GetDriveName(strDrive))
'Use the drive properties to get drive any informaiton 
strDriveInformation = "Drive" & UCase(strDrive) & " -"
strDriveInformation = strDriveInformation & "Free Space :" & FormatNumber(objDrive.FreeSpace/1024,0)
strDriveInformation = strDriveInformation & "KB"
MsgBox strDriveInformation

2)文件与文件夹的操作

经常要对一些特定文件进行搜索,获取文件的信息。Folder和File对象中的方法和属性如下图所示:

Folder

File

举几个例子:

  • 从系统文件中获取信息及删除文件
Option Explicit
Dim strPath,strFileInfo
strPath = "d:\log.txt"
Function get_file_informaiton(strPath)
	Dim objFSO, objFile
	
	'Get the File
	Set objFSO = CreateObject("scripting.filesystemobject")
	Set objFile = objFSO.GetFile(strPath)
	
	'Get any file information
	strFileInfo = strFileInfo & objFile.Name & objFile.Size & _
				& objFile.Type & objFile.DateCreated
	
	'Delete the file
	objFSO.DeleteFile(strPath)
End Function 
get_file_informaiton(strPath)
'Show the file
MsgBox strFileInfo
  • 获取文件夹里所有文件信息

在这里,我们要注意这里面有两种Case:(1)该文件中只有文件;(2) 该文件夹中不光有文件还有子文件夹,并且子文件夹中,还有文件

Option Explicit
Dim strFolderPath
strFolderPath = "D:\kai"
Function Get_folder_info(strFolderPath)
	Dim objFSO, objFolder,objFile, colFiles, 
	Dim colSubFolders,oSubFolder
	
	'Call FSO Model
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFolder = objFSO.GetFolder(strFolderPath)
	
	'Tranverse the relative file in the current folder
	Set colFiles = objFolder.Files
	
	For Each objFile In colFiles 
		Select Case objFile.Name
			Case "kai.txt"
				MsgBox "kai.txt File Existed!"
			Case "she.txt"
				MsgBox "she.txt File Existed!"
			Case "dd.txt"
				MsgBox "dd.txt File Existed!"
		End Select
	Next 
	
	'Tranverse the sub folder if the subfolders are existed.
	Set colSubFolders = objFolder.SubFolders
	
	For Each oSubFolder In colSubFolders
		Get_folder_info(oSubFolder)
	Next 
	
End Function 
Get_folder_info(strFolderPath)

3)读写Txt文件的操作

创建和打开Txt文件,主要分为两种方法:

  • OpenTextFile
  • CreateTextFile

而对文件的读写主要利用FSO中的TextStream对象来完成的。TextStream对象如下图所示:

 TextStream

通过一段代码来实践一下:

  • 写文件

 

Option Explicit
Const ForReading = 1, ForWriting =2 , ForAppending = 8
Const TristateUseDefault = -2
Dim objFSO, objFile, txtFile, txtStreamText
Dim strTxtFilePath
strTxtFilePath = "D:\Account.txt"
Set objFSO = CreateObject("scripting.filesystemobject")
'create the file by the objFSO
'Judge the file whether it is existed
If (objFSO.FileExists(strTxtFilePath)) = 0 Then
	'Create the file if there is no the text file
	objFSO.CreateTextFile("D:\Account.txt")
	Set objFile = objFSO.GetFile("D:\Account.txt")
	'Open the txt file
	Set txtFile = objFile.OpenAsTextStream(2, TristateUseDefault)
	txtFile.Write "Hello,world!"
	txtFile.Close
Else 
	'open the text file directly if the file is existed
	Set txtFile = objFSO.OpenTextFile(strTxtFilePath, 8, False)
	txtFile.WriteBlankLines 2
	txtFile.Write "2.Hello,world!"
	txtFile.Close
End If 
posted @ 2010-07-10 12:48  GeneJiang  阅读(4651)  评论(0编辑  收藏  举报