方便好用的FileSystemObject
從VB6.0起,VB新增了FileSystemObject物件,這個物件可以取代以往許多要用API才能做到的事,如覆製資料夾、取得磁碟機資訊等,在作一些相關處理時可以節省不少時間。要引用這個項目你必須以設定引用項目將 Microsoft Scripting Runtime手動加入專案,否則是沒有辦法使用的!我來概略說明一下使用的方法和功能
Dim fso as New FileSystemObject
FileSystemObject本身就已經有許多的方法可以使用了
包括資料夾複製、檔案複製
fso.CopyFolder "C:\ABC","C:\DEF"
fso.FileCopy "c:\123.txt","c:\456.txt"
建立資料夾
fso.CreateFolder "C:\ABC"
建立文字檔
fso.CreateTextFile "c:\123.txt"
刪除資料夾、檔案
fso.DeleteFolder "C:\ABC"
fso.DeleteFile "c:\123.txt"
取得Windows路徑,System路徑
fso.GetSpecialFolder(WindowsFolder)
fso.GetSpecialFolder(SystemFolder)
取得所有的磁碟機
磁碟機總數(包含網路磁碟機等) - fso.Drives.Count
取得所有磁碟機物件
Dim Drivetemp As Drive
ReDim DriveObject(1 To fso.Drives.Count)
Dim i As Integer
For Each Drivetemp In fso.Drives
i = i + 1
Set DriveObject(i) = Drivetemp
Next
經由該物件的屬性可以得知許多事,列舉常用的幾個
DriveObject的使用說明
查詢該磁碟機的可用空間
.AvailableSpace .FreeSpace
這兩個屬性都可以查出剩餘空間,我還看不出有什麼不同
查詢該磁碟機的總容量
.TotalSize
磁碟機的代碼
.DriveLetter .RootPath
DriveLetter只有代碼,RootPath則可查出根目錄的路徑
磁碟機的類別
.DriveType
傳回值分別代表
Unknown - "無從判斷"
Removable - "抽取式磁碟"
Fixed - "硬碟"
Remote - "遠端(網路)儲存裝置"
CDRom - "光碟機"
RamDisk - "RAM Disk"
取得資料夾的資料
Dim fs As New FileSystemObject ' 建立 FileSystemObject
Dim fd As Folder ' 宣告 Folder 物件
Dim i As Integer
Set fd = fs.GetFolder("c:\")
取得子資料夾的總數 - fd.SubFolders.Count
ReDim sfd(1 To fd.SubFolders.Count) As Folder
For Each fd In fd.SubFolders
i = i + 1
Set sfd(i) = Folder
List1.AddItem sfd(i).Name
Next
這個方法只能取得一層子資料夾,如果要取得所有資料夾則
須利用遞迴重覆執行
Folder的屬性比較特殊的是ShortPath可以直接取得該
Folder的短檔名及DateCreated、DateLastAccessed、
DateLastModified可取得建立時間及變更時間等
至於File物件只要改用GetFile方法,其餘沒有什麼特別不同的方法
2008年9月4日 星期四
2008年3月20日 星期四
用Vbscript 改檔名
Set objFSO = CreateObject("Scripting.FileSystemObject")
SourceFile=Server.MapPath("c:/test.asp") '要改的原始檔
TargetFile=Server.MapPath("c:/test1.asp") '新的檔名
objFSO.MoveFile SourceFile, TargetFile
SourceFile=Server.MapPath("c:/test.asp") '要改的原始檔
TargetFile=Server.MapPath("c:/test1.asp") '新的檔名
objFSO.MoveFile SourceFile, TargetFile
在DTS中使用Activex 工作處理檔案--Copy
'**********************************************************************
' Visual Basic ActiveX Script
'************************************************************************
Function Main()
dim fs,strfullpath,f
set fs=createobject("Scripting.FileSystemObject")
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim f2
dim yyymm
Set f2 = fs.OpenTextFile("D:\SKBANK\BISsys\DATA\lnctbfy.dat", ForReading, True)
yyymm = f2.Read(5)
'Main=DTSTaskExecResult_Failure
fs.CopyFile "D:\SKBANK\BISsys\DATA\lnctbfy.dat", "D:\SKBANK\BISsys\DATA\"+yyymm+"lnctbfy.dat"
set fs=nothing
Main=DTSTaskExecResult_Success
End Function
' Visual Basic ActiveX Script
'************************************************************************
Function Main()
dim fs,strfullpath,f
set fs=createobject("Scripting.FileSystemObject")
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim f2
dim yyymm
Set f2 = fs.OpenTextFile("D:\SKBANK\BISsys\DATA\lnctbfy.dat", ForReading, True)
yyymm = f2.Read(5)
'Main=DTSTaskExecResult_Failure
fs.CopyFile "D:\SKBANK\BISsys\DATA\lnctbfy.dat", "D:\SKBANK\BISsys\DATA\"+yyymm+"lnctbfy.dat"
set fs=nothing
Main=DTSTaskExecResult_Success
End Function
在DTS中使用Activex 工作處理檔案
'**********************************************************************
' Visual Basic ActiveX Script
'************************************************************************
Function Main()
dim fs,strfullpath,f
set fs=createobject("Scripting.FileSystemObject")
if (fs Is Nothing) then
Main=DTSTaskExecResult_Failure
'msgbox "1"
exit function
end if
strfullpath="D:\SFBAFK\BISsys\DATA\lnctbtt.dat"
if not fs.fileexists(strfullpath) then
Main=DTSTaskExecResult_Failure
'msgbox "2"
set fs=nothing
exit function
end if
Set f = fs.GetFile(strfullpath)
if f.size =0 then
Main=DTSTaskExecResult_Failure
'msgbox ""
set fs=nothing
exit function
end if
set fs=nothing
'msgbox "4"
'Main=DTSTaskExecResult_Failure
Main=DTSTaskExecResult_Success
End Function
' Visual Basic ActiveX Script
'************************************************************************
Function Main()
dim fs,strfullpath,f
set fs=createobject("Scripting.FileSystemObject")
if (fs Is Nothing) then
Main=DTSTaskExecResult_Failure
'msgbox "1"
exit function
end if
strfullpath="D:\SFBAFK\BISsys\DATA\lnctbtt.dat"
if not fs.fileexists(strfullpath) then
Main=DTSTaskExecResult_Failure
'msgbox "2"
set fs=nothing
exit function
end if
Set f = fs.GetFile(strfullpath)
if f.size =0 then
Main=DTSTaskExecResult_Failure
'msgbox ""
set fs=nothing
exit function
end if
set fs=nothing
'msgbox "4"
'Main=DTSTaskExecResult_Failure
Main=DTSTaskExecResult_Success
End Function
訂閱:
文章 (Atom)