Working with files
Scripting 允許用FileSystemObject (FSO)對象模式來處理磁碟機、檔案夾和檔案
要用 FileSystemObject (FSO) 對象模式來編程,則:
- 使用 CreateObject 方法來建立 FileSystemObject 對象。
- 在新建立的對象上使用適當的方法。
- 訪問對象的屬性。
FSO 對象模式包含在 Scripting 類型庫中,該庫位於 Scrrun.DLL 檔案中。
ListFiles.vbs
Option 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. |