VBS: FSO对象及文件读写

本文详细介绍了如何使用VBScript中的FileSystemObject(FSO)对象进行文件和文件夹的访问、创建、查询、读写等操作,包括驱动器操作、文件与文件夹操作以及TXT文件的读写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 

转载于:https://www.cnblogs.com/Tcorner/archive/2010/07/10/1774804.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值