VBS指令碼程式設計(7)——檔案與資料夾的操作
阿新 • • 發佈:2021-06-24
FileSystemObject 物件
FileSystemObject物件的作用:提供對計算機檔案系統的訪問,它允許我們在程式碼內操作文字檔案、資料夾及驅動器。FileSystemObject物件提供一個屬性和一系列方法,可用它們來操縱FileSystemObject物件實現的一些從屬物件。這裡提供了全部的內容概要,然後介紹每一個從屬物件。FileSystemObject物件的屬性
FileSystemObject物件只有一個屬性,它用於得到當前機器上的所有有效驅動器的列表,如表1所示:' 建立FileSystemObject物件
Set objFSO = CreateObject("Scripting.FileSystemObject")
' 建立Drives集合,並賦值給colDrives
Set colDrives = objFSO.Drives
' 遍歷Drives集合
For Each objDrive in colDrives
'判斷驅動器是否已準備好
If objDrive.IsReady Then
msg = "驅動器的名稱為:" & objDrive.DriveLetter & VBCrlf
msg =msg & "驅動器檔案系統的型別:" & objDrive.FileSystem & VBCrlf
msg = msg & "驅動器的總容量:" & int(objDrive.TotalSize/(1024*1024*1024)) & "G "& VBCrlf
msg = msg & "剩餘空間的總量:" & int(objDrive.FreeSpace/(1024*1024*1024)) & "G "
End If
Next
msgbox msg
備註:在系統上執行這段程式以前有一點要注意。如果在A驅動器裡沒有磁碟,或CD-ROM驅動器裡沒有光碟,將得到一個錯誤提示:“Disk Not Ready”。除了DriveLetter屬性和DriveType屬性外,在使用其他屬性和方法前,通過檢查每個驅動器的IsReady屬性,可以保護該頁面。
FileSystemObject物件的方法
- 與驅動器有關的方法
' 建立FileSystemObject物件
Set objFSO = CreateObject("Scripting.FileSystemObject")
For intCode = 65 To 90 'ANSI codes for 'A' to 'Z'
strLetter = Chr(intCode) 'Chr:返回與指定的 ANSI 字元程式碼相對應的字元。
If objFSO.DriveExists(strLetter) Then '判斷相應的碟符是否存在,如存在輸出
MsgBox "存在 "&strLetter&": 驅動器"
End If
Next
- 與資料夾有關的方法
' 建立FileSystemObject物件
Set objFSO = CreateObject("Scripting.FileSystemObject")
' 將D:\Test下所有的內容複製到C:\123
objFSO.CopyFolder "D:\Test","C:\123"
CreateFolder及DeleteFolder的使用
方法名:CreateFolder(foldername)/DeleteFolder(folderspec,force)
說明:建立/刪除一個路徑名為 foldername 的資料夾。
示例:
' 建立FileSystemObject物件
Set objfso = createobject("scripting.FileSystemObject")
‘ 在c:/建立new folder資料夾,如果資料夾存在,則會出現錯誤提示
objfso.CreateFolder("c:/new folder")
‘ 刪除c:/new folder資料夾
objfso.DeleteFolder("c:/new folder")
注:DeleteFolder無論資料夾是否包含內容,都將刪除該資料夾
FolderExists的使用
方法名:FolderExists(folderspec)
說明:如果指定的資料夾存在,則返回 True;否則返回 False。
示例:
Function hanshu(strpath)
Dim objFSO,objSet
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strpath) Then
objSet = objFSO.CreateFolder(strpath)
MsgBox "資料夾建立成功"
Else
objSet = objFSO.DeleteFolder(strpath)
MsgBox "資料夾刪除成功"
End If
hanshu = objSet
End Function
path = InputBox("請輸入資料夾路徑")
hanshu(path)
GetAbsolutePathName的使用
方法名:GetAbsolutePathName(pathspec)
說明:從提供的指定路徑中返回完整且含義明確的路徑。
示例:
Dim objFSO
'建立FileSystemObject物件
Set objFSO = CreateObject("Scripting.FileSystemObject")
'返回當前的目錄,下面兩條語句返回一樣的結果
MsgBox(objFSO.GetAbsolutePathName(""))
MsgBox(objFSO.GetAbsolutePathName("c:"))
GetFolder的使用
方法名:GetFolder(folderspec)
說明:返回與指定的路徑中某資料夾相應的 Folder 物件。可以根據所返回的Folder 物件,再去訪問該Folder 物件所持有的屬性。
示例:
Dim objFSO,objSet,strPath
'建立FileSystemObject物件
Set objFSO = CreateObject("Scripting.FileSystemObject")
strPath = InputBox("請輸入資料夾的路徑")
Set objSet = objFSO.GetFolder(strPath)
' 根據所返回的Folder物件,去訪問其相關的屬性
MsgBox("資料夾建立的日期:"&objSet.DateCreated&vbCrLf&"資料夾所在驅動:"&objSet.Drive&vbCrLf&"資料夾名稱:"&objSet.Name)
GetParentFolderName的使用
方法名:GetParentFolderName(pathspec)
說明:返回字串,該字串包含指定的路徑中最後一個檔案或資料夾的父資料夾。
示例:
Dim objFSO
'建立FileSystemObject物件
Set objFSO = CreateObject("Scripting.FileSyStemObject")
'使用GetParentFolderName方法來返回上一層資料夾
MsgBox(objFSO.GetParentFolderName("C:\Windows\System32"))
GetSpecialFolder的使用
方法名:GetSpecialFolder(folderspec)
說明:返回指定的特殊資料夾。
示例:
Dim objFSO,tempName
Set objFSO = CreateObject("Scripting.FileSystemObject")
'GetSpecialFolder()中的引數2,返回一個名為Temp的臨時資料夾
Set tempName = objFSO.GetSpecialFolder(2)
MsgBox tempName
MoveFolder的使用
方法名:MoveFolder(source,destination)
說明:將一個或多個資料夾從某位置移動到另一位置。
示例:
Dim Source,Destination
Source = InputBox("請輸入原資料夾")
Destination = InputBox("請輸入目標資料夾")
'呼叫MoveFolders方法
Call MoveDir(Source,Destination)
Sub MoveDir(s,d)
Dim FSO
'建立FileSystemObject物件
Set FSO = CreateObject("Scripting.FileSystemObject")
'使用MoveFolder方法,將原資料夾移動到目標資料夾
FSO.MoveFolder s,d
End Sub
注:如果指令碼在執行前目標資料夾已存在,則執行是會發生錯誤;在不同的磁碟下MoveFolder方法不能用
上面提到的Folder方法是基於FileSystemObject物件的,現在我們來看看基於Folder物件的方法。
Folder子物件的方法
Folder物件提供一組可用於複製、刪除和移動當前資料夾的方法。這些方法的執行方式與FileSystemObject物件的CopyFolder、DeleFolder和MoveFolder方法相同,但這些方法不要求source引數,因為原始檔就是這個資料夾。 備註:CopyFolder、DeleFolder等方法是基於FileSystemObject物件的;而下面的Copy、Delete等方法是基於FileSystemObject物件下的Folder物件,他們實現的功能是類似的。Dim Source,Destination
Source = InputBox("請輸入原資料夾")
Destination = InputBox("請輸入目標資料夾")
Call Copyx(Source,Destination)
Sub Copyx(s,d)
Dim FSO,objSet
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objSet = FSO.GetFolder(s) '使用GetFolder方法來返回Folder物件
objSet.Copy(d) '使用Copy方法將原資料夾中的內容複製到目標資料夾中
End Sub
備註:如果目標資料夾不存在,則指令碼在執行是將自動建立
Delete的使用
方法名:Delete(force)
說明:刪除資料夾及裡面的所有內容。
示例:
Dim path
path = InputBox("請輸入要刪除的資料夾")
Call Del(path)
Sub Del(p)
Dim FSO,ObjSet
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ObjSet = FSO.GetFolder(p)
ObjSet.Delete(True)
End Sub
備註:如果資料夾屬性為只讀且Delete的引數為False,則指令碼在執行是將提示“沒有許可權”;當資料夾不存在時,將提示“路徑未找到”。
Move的使用
方法名:Move(destination)
說明:將資料夾及裡面所有的內容移動到 destination 指定的資料夾。
示例:
Dim Source,Destination
Source = InputBox("請輸入原資料夾")
Destination = InputBox("請輸入目標資料夾")
Call Movex(Source,Destination)
Sub Movex(s,d)
Dim FSO,objSet
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objSet = FSO.GetFolder(s)
objSet.Move(d)
End Sub
CreateTextFile的使用
方法名:CreateTextFile(filename,overwrite,unicode)
說明:建立指定檔案並返回 TextStream 物件,該物件可用於讀或寫建立的檔案。
示例:
Dim strPath,strFile
strPath = InputBox("請輸入建立資料夾的路徑")
strFile = InputBox("請輸入檔名稱")
Call CreateFilex(strPath,strFile)
Sub CreateFilex(path,file)
Dim FSO,objSet
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objSet = FSO.GetFolder(path)
objSet.CreateTextFile file,True
End Sub
備註:CreateTextFile()所建立的檔案型別是根據所提供的副檔名來確定的,FileSystemObject物件也可以呼叫CreateTextFile()
Folder子物件的屬性
Dim path
path = InputBox("請輸入資料夾路徑")
Call FolderAttributes(path)
Sub FolderAttributes(sourcepath)
Dim FSO,objSet
Set FSO = CreateObject("Scripting.FileSystemObject")
'使用GetFolder方法來返回Folder物件
Set objSet = FSO.getFolder(sourcepath)
'通過Folder物件來訪問Folder屬性
MsgBox ("資料夾所在磁碟為:"&objSet.Drive&vbCrLf&"資料夾建立的日期:"&objSet.DateCreated&vbCrLf&"資料夾的名稱為:"&objSet.Name)
End Sub
SubFolers屬性的使用
示例:
Dim path
path = InputBox("請輸入資料夾路徑")
Call FolderAttributes(path)
Sub FolderAttributes(sourcepath)
Dim FSO,objSet,objFolders,objFolder,FolderName,i
Set FSO = CreateObject("Scripting.FileSystemObject")
'使用GetFolder方法來返回Folder物件
Set objSet = FSO.getFolder(sourcepath)
'SubFolders屬性將返回所有子資料夾對應的Folder集合
Set objFolders = objSet.SubFolders
i = 0
For Each objFolder In objFolders
i = i+1
FolderName = FolderName&objFolder.Name&vbCr
Next
MsgBox (sourcepath&"路徑下共有"&i&"個資料夾,資料夾的名稱為:"&vbCrLf&FolderName)
End Sub
Files屬性的使用
示例:
Dim path
path = InputBox("請輸入資料夾路徑")
Call FolderAttributes(path)
Sub FolderAttributes(sourcepath)
Dim FSO,objSet,objFiles,objFile,FileName,i
Set FSO = CreateObject("Scripting.FileSystemObject")
'使用GetFolder方法來返回Folder物件
Set objSet = FSO.getFolder(sourcepath)
'遍歷Files集合並顯示資料夾中所有的檔名
Set objFiles = objSet.Files
i = 0
For Each objFile In objFiles
i = i+1
FileName = FileName&"("&i&")"&objFile.Name&vbCr
Next
MsgBox (sourcepath&"路徑下共有"&i&"個檔案,檔案的名稱為:"&vbCrLf&FileName)
End Sub
- 與檔案有關的方法及屬性
Dim sourcepath,targetpath
sourcepath = "C:/testing/*.txt"
targetpath = "C:/123/"
Call FolderAttributes(sourcepath,targetpath)
Sub FolderAttributes(sourcepath,targetpath)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
'將testing資料夾下所有副檔名名為.txt的檔案,複製到123資料夾下
fso.CopyFile sourcepath,targetpath
fso.MoveFile sourcepath,targetpath
End Sub
備註:如果C:/123資料夾不存在,則指令碼執行時提示“路徑不存在”
CreateTextFile及DeleteFile的使用
方法名:CreateTextFile(filename,overwrite,unicode)
說明:建立指定檔案並返回 TextStream 物件,該物件可用於讀或寫建立的檔案。
方法名:DeleFile(filespec,force)
說明:刪除指定的檔案。
示例:
Dim sourcepath
sourcepath = "C:/testing/ myClass2.doc "
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
'在testing資料夾下,使用CreateTextFile()建立myClass2.doc檔案
fso.CreateTextFile sourcepath,true
'刪除testing資料夾下,所有副檔名為.txt的檔案
fso.DeleteFile sourcepath,true
End Sub
FileExists的使用
方法名:FileExists(filespec)
說明:判斷所指定的檔案是否存在。此方法將返回Bool值。
示例:
Dim sourcepath
sourcepath = "C:/testing/test.txt"
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
'判斷C:/testing/test.txt是否有存在,如果存在返回“true”;否則返回“false”
If fso.FileExists(sourcepath) Then
msgbox "true"
else
msgbox "false"
End If
End Sub
GetBaseName、GetFileName及GetExtensionName的使用
方法名:GetBaseName(filespec)
說明:返回字串,檔案 (不帶副檔名), 或者提供的路徑說明中的資料夾。
方法名:GetExtensionName(filespec)
說明:返回字串,該字串包含路徑最後一個組成部分的副檔名。
方法名:GetFileName(pathspec)
說明:返回指定路徑(不是指定驅動器路徑部分)的最後一個檔案或資料夾。
示例:
Dim sourcepath
sourcepath = "C:/testing/test.txt"
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
'返回檔名test, 副檔名 txt, 檔名+副檔名 test.txt
Msgbox (fso.GetBaseName(sourcepath))
Msgbox (fso. GetExtensionName(sourcepath))
Msgbox (fso.GetFileName(sourcepath))
End Sub
GetFile的使用
方法名:GetFile(filespec)
說明:返回與指定路徑中某檔案相應的 File 物件,既而可以訪問到File的屬性。
示例:
Sub FolderAttributes(sourcepath)
Dim fso,objFile
Set fso = CreateObject("Scripting.FileSystemObject")
'呼叫GetFile()返回File物件,通過File物件來訪問其下的屬性
set objFile = fso.GetFile(sourcepath)
MsgBox("該檔案所在的碟符為:"&objFile.Drive&vbCrLf&"檔名為:"&objFile.Name)
End Sub
GetTempName的使用
方法名:GetTempName()
說明:返回隨機生成的臨時檔案或資料夾的名稱,用於執行要求臨時檔案或資料夾的操作。
示例:
Dim fso, tempfile
'建立FileSystemObject物件
Set fso = CreateObject("Scripting.FileSystemObject")
'呼叫CreateTempFile函式,並將物件賦值給tempfile
Set tempfile = CreateTempFile
'在臨時檔案中寫入字元
tempfile.WriteLine "世界你好"
'關閉物件
tempfile.Close
Function CreateTempFile
Dim tfolder, tname, tfile
Const TemporaryFolder = 2
'GetSpecialFolder()中的引數2,返回一個名為Temp的臨時資料夾
Set tfolder = fso.GetSpecialFolder(TemporaryFolder)
'隨機生成一個臨時檔名
tname = fso.GetTempName
'在Temp臨時資料夾下面,建立一個臨時檔案
Set tfile = tfolder.CreateTextFile(tname)
'將結果賦值給函式
Set CreateTempFile = tfile
End Function
OpenTextFile的使用
方法名:(filename,iomode,create,format)
說明:開啟指定的檔案並返回一個 TextStream 物件,可以讀取、寫入此物件或將其追加到檔案。
示例:
Dim path
path = InputBox("請輸入檔案的位置")
strContent = InputBox("請輸入追加的內容")
'三個引數的分別為:以只讀模式開啟檔案、以只寫方式開啟檔案、開啟檔案並在檔案末尾進行寫操作
Const intForReading = 1,intForWriting = 2,intForAppending = 8
'呼叫OpenTextFileTest()
Call OpenText(path,intForAppending,strContent)
Sub OpenText(path,intForAppending,strContent)
Dim FSO,objTStream
'建立FileSystemObject物件
Set FSO = CreateObject("Scripting.FileSystemObject")
'引數true表示:如果檔案不存在,則建立檔案。最後OpenTextFile()將返回TextStream物件
Set objTStream = FSO.OpenTextFile(path,intForAppending,True)
'利用TextStream物件來將字串寫入檔案
objTStream.Write strContent
'釋放TextStream物件
objTStream.Close
End Sub
File子物件的屬性
Dim sourcepath
sourcepath = InputBox("請輸入檔案所在的路徑")
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso,objFile
Set fso = CreateObject("Scripting.FileSystemObject")
'呼叫GetFile()返回File物件,通過File物件來訪問其下的屬性
set objFile = fso.GetFile(sourcepath)
MsgBox("該檔案所在的碟符為:"&objFile.Drive&vbCrLf&"檔名為:"&objFile.Name)
End Sub
備註:File物件屬性的使用方法和前面介紹的Folder物件屬性的使用方法一樣,所以在這不一一列出。
TextStream物件
TextStream物件是用於訪問文字檔案的物件,它是FIleSystemObject一個獨立的附屬物件,但在使用TextStream物件時,我們仍要藉助FileSystemObject物件或其附屬物件來建立一個TextStream物件訪問磁碟檔案的內容。可以通過FileSystemObject物件的CreateTextFile()及OpenTextFile(),來獲取TextStream的物件控制代碼。 下面我們來具體的看看TextStream 物件的方法及屬性的使用。TextStream物件的方法
Dim strPath,strText
strPath = "c:\users\text.txt"
strText = "This is a test"
Call CreateFile(strPath,strText)
Sub CreateFile(Path,Text)
Dim objFSO,objStream
'建立FileSystemObject物件
Set objFSO = CreateObject("Scripting.FileSystemObject")
'使用FileSystemObject物件的CreateTextFile(),來返回一個TextStream物件控制代碼
Set objStream = objFSO.CreateTextFile(Path,True)
'三個Write的意思分別為:寫入3個換行符、寫入帶換行符的字元、在文字中寫入字元
objStream.WriteBlankLines 3
objStream.WriteLine(Text)
objStream.Write(Text)
'關閉TextStream物件
objStream.Close
End Sub
Read、ReadAll及ReadLine的使用
方法名:Read(numchars)
說明:從 TextStream 檔案中讀入指定數目的字元並返回結果字串。
方法名:ReadAll()
說明:讀入全部 TextStream 檔案並返回結果字串。
方法名:ReadLine()
說明:從 TextStream 檔案中讀入一整行字元(直到下一行,但不包括下一行字元),並返回結果字串。
示例:
Sub CreateFile(strPath,strText)
Dim objFso,objStream
'建立FileSystemObject物件
Set objFso = CreateObject("Scripting.FileSystemObject")
'使用FileSystemObject物件的CreateTextFile(),來返回一個TextStream物件控制代碼
Set objStream = objFso.CreateTextFile(strPath,True)
'寫入字元
objStream.WriteLine(strText)
Set objStream = objFso.OpenTextFile(strPath,1,true)
msgbox (objStream.ReadLine)
'或者 msgbox (objStream.ReadAll)
'或者 msgbox (objStream.Read(Len(strText)))
'關閉TextStream物件
objStream.Close
End Sub
Skip、SkipLine的使用
方法名:Skip(numchars)
說明:讀取 TextStream 檔案時跳過指定數目的字元
方法名:SkipLine()
說明:當讀到 TextStream 檔案時,跳過下一行。
示例:
Dim strPath,strText
strPath = "C:/testing.txt"
Call CreateFile(strPath)
Sub CreateFile(strPath)
Dim objFso,objStream
'建立FileSystemObject物件
Set objFso = CreateObject("Scripting.FileSystemObject")
'使用FileSystemObject物件的CreateTextFile(),來返回一個TextStream物件控制代碼
Set objStream = objFso.CreateTextFile(strPath,True)
'在文字中寫入字元
objStream.Write "This is Test !" & vbCrLf & "hello word !"
'以只讀的方式開啟檔案
Set objStream = objFso.OpenTextFile(strPath,1,true)
'讀取檔案時跳過5個字元;或者跳過當前行,讀取下一行
objStream.Skip(5)
'objStream.SkipLine
'讀取文字內容
msgbox objStream.ReadAll
'關閉TextStream物件
objStream.Close
End Sub
備註:兩者的區別是:Skip——跳過指定的幾個字元;SkipLine——跳過一行
TextStream物件的屬性
TextStream 的屬性提供有關檔案內檔案指標當前位置的資訊,如表9所示。注意,所有的屬性是隻讀的。Dim strPath,strText
strPath = "c:\test.txt"
Call ReadFile(strPath)
Sub ReadFile(Path)
Dim objFSO,objStream,stri
Set objFSO = CreateObject("Scripting.FileSystemobject")
'以只讀的方式開啟檔案
Set objStream = objFSO.OpenTextFile(Path,1,True)
'如果當前的指標不在行末,則讀取文字內容
Do While objStream.AtEndOfLine <> True
'如果當前指標不在文字末尾 'Do while objStream.AtEndOfStream <> true
stri = stri + objStream.Read(1)
Loop
MsgBox stri
objStream.Close
End Sub
備註:兩者間的區別是:AtEndOfLine——讀取到當前文字行的末尾;AtEndOfStream——讀取到整個文字的末尾
Column及Line的使用
示例:
Sub TestTextStream(strPath)
Dim objFso,objTStream,str
Set objFso = CreateObject("Scripting.FileSystemObject")
'以只讀的方式開啟檔案
Set objTStream = objFso.OpenTextFile(strPath,1)
'如果當前的指標不在整個文件的末尾,讀取文字的所有內容
Do While objTStream.AtEndOfStream <> true
objTStream.ReadAll
str = str + "共有" & objTStream.Line & "行資料,游標最後所在列號為:" & objTStream.Column & vbCrLf
Loop
'列印資訊
print str
End Sub