VBS: FSO对象及文件读写
1. FSO概述
文件系统是所有操作系统最重要的部分之一,脚本经常会需要对文件及文件夹进行访问和管理,在Vbs 中对桌面和文件系统进行访问的顶级对象是FileSystemObject(FSO),这个对象特别复杂,是vbs 进行文件操作的核心。
FileSytemObject是一种主控对象,通过它可以访问一系列的对象。FileSystemObject中所有对象一起提供了访问和操作Windows文件系统的功能。
FSO对象模型主要由以下的对象和集合组成的。
2. FSO 操作模型
现在我们遇到如下的一些问题:
- 如何通过FSO来查询文件和文件夹
- 如何通过FSO来创建Text文件并且对文件进行各种操作
- 如何来查询各种驱动器的信息
我们可以通过如下的一个模型图,来很快的做到这些事情:
1) 驱动器的操作
获取某一个驱动器的信息,如它的大小,名字,文件系统 等等,在这里我们主要使用驱动器对象,该对象没有方法,只有属性。
它的主要属性主要如下图所示:
用一段代码,来演示一下如何对Drive对象和Drives集合进行操作。
- 这段代码的主要作用是看看C驱动器的相关信息
Option ExplicitDim objFSO, objDriveDim strDriveDim strTotalsize,strVolumeNameDim strDriveInformationstrDrive = "C:"'call FileSystemObject ModelSet objFSO = CreateObject("scripting.filesystemobject")'Get the Drive NameSet objDrive = objFSO.GetDrive(objFSO.GetDriveName(strDrive))'Use the drive properties to get drive any informaitonstrDriveInformation = "Drive" & UCase(strDrive) & " -"strDriveInformation = strDriveInformation & "Free Space :" & FormatNumber(objDrive.FreeSpace/1024,0)strDriveInformation = strDriveInformation & "KB"MsgBox strDriveInformation
2)文件与文件夹的操作
经常要对一些特定文件进行搜索,获取文件的信息。Folder和File对象中的方法和属性如下图所示:
举几个例子:
- 从系统文件中获取信息及删除文件
Option ExplicitDim strPath,strFileInfostrPath = "d:\log.txt"Function get_file_informaiton(strPath)Dim objFSO, objFile'Get the FileSet objFSO = CreateObject("scripting.filesystemobject")Set objFile = objFSO.GetFile(strPath)'Get any file informationstrFileInfo = strFileInfo & objFile.Name & objFile.Size & _& objFile.Type & objFile.DateCreated'Delete the fileobjFSO.DeleteFile(strPath)End Functionget_file_informaiton(strPath)'Show the fileMsgBox strFileInfo
- 获取文件夹里所有文件信息
在这里,我们要注意这里面有两种Case:(1)该文件中只有文件;(2) 该文件夹中不光有文件还有子文件夹,并且子文件夹中,还有文件
Option ExplicitDim strFolderPathstrFolderPath = "D:\kai"Function Get_folder_info(strFolderPath)Dim objFSO, objFolder,objFile, colFiles,Dim colSubFolders,oSubFolder'Call FSO ModelSet objFSO = CreateObject("Scripting.FileSystemObject")Set objFolder = objFSO.GetFolder(strFolderPath)'Tranverse the relative file in the current folderSet colFiles = objFolder.FilesFor Each objFile In colFilesSelect Case objFile.NameCase "kai.txt"MsgBox "kai.txt File Existed!"Case "she.txt"MsgBox "she.txt File Existed!"Case "dd.txt"MsgBox "dd.txt File Existed!"End SelectNext'Tranverse the sub folder if the subfolders are existed.Set colSubFolders = objFolder.SubFoldersFor Each oSubFolder In colSubFoldersGet_folder_info(oSubFolder)NextEnd FunctionGet_folder_info(strFolderPath)
3)读写Txt文件的操作
创建和打开Txt文件,主要分为两种方法:
- OpenTextFile
- CreateTextFile
而对文件的读写主要利用FSO中的TextStream对象来完成的。TextStream对象如下图所示:
通过一段代码来实践一下:
- 写文件
Option ExplicitConst ForReading = 1, ForWriting =2 , ForAppending = 8Const TristateUseDefault = -2Dim objFSO, objFile, txtFile, txtStreamTextDim strTxtFilePathstrTxtFilePath = "D:\Account.txt"Set objFSO = CreateObject("scripting.filesystemobject")'create the file by the objFSO'Judge the file whether it is existedIf (objFSO.FileExists(strTxtFilePath)) = 0 Then'Create the file if there is no the text fileobjFSO.CreateTextFile("D:\Account.txt")Set objFile = objFSO.GetFile("D:\Account.txt")'Open the txt fileSet txtFile = objFile.OpenAsTextStream(2, TristateUseDefault)txtFile.Write "Hello,world!"txtFile.CloseElse'open the text file directly if the file is existedSet txtFile = objFSO.OpenTextFile(strTxtFilePath, 8, False)txtFile.WriteBlankLines 2txtFile.Write "2.Hello,world!"txtFile.CloseEnd If

浙公网安备 33010602011771号