VBscript (一) Working with files
Working with files
Scripting 允许用FileSystemObject (FSO)对象模式来处理驱动器、文件夹和文件
要用 FileSystemObject (FSO) 对象模式来编程,则:
FSO 对象模式包含在 Scripting 类型库中,该库位于 Scrrun.dll 文件中。
ListFiles.vbsOption Explicit
On Error Resume Next
Dim FolderPath 'path to the folder to be searched for files
Dim objFSO 'the FileSystemObject
Dim objFolder 'the folder object
Dim colFiles 'collection of files from files method
Dim objFile 'individual file object
FolderPath = "c:\fso"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(FolderPath)
Set colFiles = objFolder.Files
For Each objFile in colFiles
WScript.Echo objFile.Name, objFile.Size & " bytes"
WScript.Echo VbTab & "created: " & objFile.DateCreated
WScript.Echo VbTab & "modified: " & objFile.DateLastModified
Next
To enumerate a list of files
1.
Use CreateObject to create the file system object.
2.
Define the folder to be searched by using GetFolder.
3.
Use the Files command to list files.
4.
Use a For Each statement to walk through the folder.
可以将程序分为以下四个部分:
Header Information主要包括三个语句: Option Explicit, On Error Resume Next, and Dim
总之, 使用Option Explicit之后,如果再使用变量则必须首先用Dim来声明.这叫做强制变量声明. On Error Resume Next,当脚本出现错误后,忽略这个错误继续执行.当调试程序时需要把这条语句关掉.Dim用来声明一个变量.
Reference Information
FolderPath = "c:\fso"
FolderPath这个变量主要作用就是使脚本在以后的修改中更加方便.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set is a command in VBScript that is used to assign an object reference to a variable.
Set的作用就是将一个对象的引用赋值给一个变量.
要用 FileSystemObject (FSO) 对象模式来编程,首先使用 CreateObject 方法来创建 FileSystemObject 对象,然后才能使用FSO的一些方法和属性.
Worker and Output Information此脚本首先建立file sytem object,然后将其赋给变量objFSO,
For Each… Next 的用法:
Just the Steps
To use For Each...Next
1.
On a new line in a script, type For Each and then a variable name.(For Each a in acollection)
2.
On the next line, enter a command you want repeated.
3.
On the line following the command you want repeated, type Next.
用来遍历集合中的每个对象
如果听到collection,首先想到的就应该是For Each…Next.
如果不知道需要循环多少次时,可以使用For Each … Next
Creating Files
Just the Steps
To create a file
1.
Use CreateObject to create an instance of FileSystemObject.
2.
Use the CreateTextFile method.
3.
Include the full path and the name of the desired file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile= objFSO.CreateTextFile("C:\FSO.txt")
Writing to a Text File
Just the Steps
To write to a text file
1.
Create an instance of FileSystemObject.
2.
Use the appropriate parameter to indicate that you are going to either overwrite the file (2) or append data to the file (8).
3.
Use either the Write, WriteLine, or WriteBlankLines method to write to the file.
4.
Close the text file.
BasicLog.vbs
LogFile = "C:\fso\fso.txt"
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(LogFile, ForWriting,True)
objFile.WriteLine "beginning process " & Now
objFile.WriteLine "working on process " & Now
objFile.WriteLine "Process completed at " & Now
objFile.Close
LogFile变量用于存,log的文件位置.方便以后更改.
ForWriting代表覆盖,每次写文件将会把以前的文件内容覆盖掉.
OpenTextFile 方法
打开指定的文件并返回一个 TextStream 对象,可以读取、写入此对象或将其追加到文件。
object.OpenTextFile(filename[, iomode[, create[, format]]])
参数
object
必选项。应为 FileSystemObject 对象的名称。
filename
必选项。字符串表达式,指明要打开的文件名称。
iomode
可选项。输入/输出模式,是下列三个常数之一:ForReading,ForWriting,或 ForAppending。
create
可选项。Boolean 值,指出当指定的 filename 不存在时是否能够创建新文件。允许创建新文件时为True,否则为False。默认值为False。
format
可选项。三个 Tristate 值之一,指出以何种格式打开文件。若忽略此参数,则文件以 ASCII 格式打开。
设置iomode 参数可为下列设置之一:
常数
值
描述
ForReading
1
以只读模式打开文件。不能对此文件进行写操作。
ForWriting
2
以只写方式打开文件。不能对此文件进行读操作。
ForAppending
8
打开文件并在文件末尾进行写操作。
〈P〉 format 参数可为下列设置之一:
常数
值
描述
TristateUseDefault
-2
以系统默认格式打开文件。
TristateTrue
-1
以 Unicode 格式打开文件。
TristateFalse
0
以 ASCII 格式打开文件。
Verifying a File Exists
实际工作中可能更容易用到,首先判断一下文件是否存在,若存在则向这个文件中增加内容.若不存在,则首先建议这个文件.其实OpenTextFile,这个方法中的第三个选项,如果设置为True,当打开一个文件,而此文件不存在时也会首先创建文件.
VerifyFileExists.vbs
LogFile = "C:\FSO\fso.txt"
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(LogFile) Then
Set objFile = objFSO.OpenTextFile(LogFile, ForAppending)
objFile.Write "appending " & Now
Else
Set objFile = objFSO.CreateTextFile(LogFile)
objFile.write "writing to new file " & now
End If
Tips,如果用CreateTextFile新建立一个文件后,不必再使用OpenTextFile打开这个文件,因为VB很智能,在建立一个文件的同时,会打开这个文件,以方便写入.所以,要记住关掉文件.
FAQ:Q.
What is required to talk to the file system by using FileSystemObject?
A.
You can use FileSystemObject by first using the CreateObject command, and then assigning to a variable the object that comes back.
Q.
Why do you want an object for FileSystemObject?
A.
You want a object for FileSystemObject because it enables you to work with files and folders.