VBscript (一) Working with files
2012-08-10 15:04
274 查看
Workingwithfiles
Scripting允许用FileSystemObject(FSO)对象模式来处理驱动器、文件夹和文件要用FileSystemObject(FSO)对象模式来编程,则:
使用CreateObject方法来创建FileSystemObject对象。
在新创建的对象上使用适当的方法。
访问对象的属性。
FSO对象模式包含在Scripting类型库中,该库位于Scrrun.dll文件中。
ListFiles.vbs
OptionExplicitOnErrorResumeNext
DimFolderPath'pathtothefoldertobesearchedforfiles
DimobjFSO'theFileSystemObject
DimobjFolder'thefolderobject
DimcolFiles'collectionoffilesfromfilesmethod
DimobjFile'individualfileobject
FolderPath="c:\fso"
SetobjFSO=CreateObject("Scripting.FileSystemObject")
SetobjFolder=objFSO.GetFolder(FolderPath)
SetcolFiles=objFolder.Files
ForEachobjFileincolFiles
WScript.EchoobjFile.Name,objFile.Size&"bytes"
WScript.EchoVbTab&"created:"&objFile.DateCreated
WScript.EchoVbTab&"modified:"&objFile.DateLastModified
Next
Toenumeratealistoffiles
1. | UseCreateObjecttocreatethefilesystemobject. |
2. | DefinethefoldertobesearchedbyusingGetFolder. |
3. | UsetheFilescommandtolistfiles. |
4. | UseaForEachstatementtowalkthroughthefolder. |
HeaderInformation
主要包括三个语句:OptionExplicit,OnErrorResumeNext,andDim总之,使用OptionExplicit之后,如果再使用变量则必须首先用Dim来声明.这叫做强制变量声明.
OnErrorResumeNext,当脚本出现错误后,忽略这个错误继续执行.当调试程序时需要把这条语句关掉.Dim用来声明一个变量.
ReferenceInformation
FolderPath="c:\fso"FolderPath这个变量主要作用就是使脚本在以后的修改中更加方便.
SetobjFSO=CreateObject("Scripting.FileSystemObject")
SetisacommandinVBScriptthatisusedtoassignanobjectreferencetoavariable.
Set的作用就是将一个对象的引用赋值给一个变量.
要用FileSystemObject(FSO)对象模式来编程,首先使用CreateObject方法来创建FileSystemObject对象,然后才能使用FSO的一些方法和属性.
WorkerandOutputInformation
此脚本首先建立filesytemobject,然后将其赋给变量objFSO,ForEach…Next
的用法:
JusttheSteps
TouseForEach...Next
|
如果听到collection,首先想到的就应该是ForEach…Next.
如果不知道需要循环多少次时,可以使用ForEach…Next
CreatingFiles
JusttheStepsTocreateafile
|
SetobjFile=objFSO.CreateTextFile("C:\FSO.txt")
WritingtoaTextFile
JusttheStepsTowritetoatextfile
|
LogFile="C:\fso\fso.txt"
ConstForWriting=2
SetobjFSO=CreateObject("Scripting.FileSystemObject")
SetobjFile=objFSO.OpenTextFile(LogFile,ForWriting,True)
objFile.WriteLine"beginningprocess"&Now
objFile.WriteLine"workingonprocess"&Now
objFile.WriteLine"Processcompletedat"&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 | 打开文件并在文件末尾进行写操作。 |
常数 | 值 | 描述 |
TristateUseDefault | -2 | 以系统默认格式打开文件。 |
TristateTrue | -1 | 以Unicode 格式打开文件。 |
TristateFalse | 0 | 以ASCII 格式打开文件。 |
VerifyingaFileExists
实际工作中可能更容易用到,首先判断一下文件是否存在,若存在则向这个文件中增加内容.若不存在,则首先建议这个文件.其实OpenTextFile,这个方法中的第三个选项,如果设置为True,当打开一个文件,而此文件不存在时也会首先创建文件.VerifyFileExists.vbs
LogFile="C:\FSO\fso.txt"
ConstForAppending=8
SetobjFSO=CreateObject("Scripting.FileSystemObject")
IfobjFSO.FileExists(LogFile)Then
SetobjFile=objFSO.OpenTextFile(LogFile,ForAppending)
objFile.Write"appending"&Now
Else
SetobjFile=objFSO.CreateTextFile(LogFile)
objFile.write"writingtonewfile"&now
EndIf
Tips,如果用CreateTextFile新建立一个文件后,不必再使用OpenTextFile打开这个文件,因为VB很智能,在建立一个文件的同时,会打开这个文件,以方便写入.所以,要记住关掉文件.
FAQ:
Q. | WhatisrequiredtotalktothefilesystembyusingFileSystemObject? |
A. | YoucanuseFileSystemObjectbyfirstusingtheCreateObjectcommand,andthenassigningtoavariabletheobjectthatcomesback. |
Q. | WhydoyouwantanobjectforFileSystemObject? |
A. | YouwantaobjectforFileSystemObjectbecauseitenablesyoutoworkwithfilesandfolders. |
相关文章推荐
- Working with CSV files in Bash
- Working with Files
- Linux: working with text files
- Working with Files
- Working with Configuration Files (app.config) in C++/CLI
- Working with iPhone files and folders
- Working with memory mapped files in .NET 4
- Working-with-files-in-javascript
- VBscript(二)Working with Folders
- 431.chapter10. working with flat files
- Working with Files in C#
- Steps to developing Metro style apps 第四章(1) Working with data and files
- Working with Files(Chapter 16 of Programming in Objective-C 2.0)
- #3 working with data stored in files && securing your application
- Working with Configuration Files (app.config) in C++/CLI
- Working with Excel Files in Python
- Working with PDF files in C# using PdfBox and IKVM
- Working with Terracotta Configuration Files
- Working with Linux Files and Folders in Bash
- Section 5: Working with Files and Directories