在ASP程式中啟用這個物件的語法是:
Set my_FSO = CreateObject("Scripting.FileSystemObject")
FSO物件在IIS4.0以上有超過20幾種方法,以下是FSO物件常用的幾種常見方法:
屬性/方法 | 說明 |
Drives屬性 | 傳回磁碟代號、磁碟類型、共享名稱、磁碟標籤等 |
CopyFile方法 | 複製檔案 |
CopyFolder方法 | 複製資料夾 |
CreatFolder方法 | 建立新資料夾 |
Deletefile 、DeleteFolder方法 | 刪除檔案或資料夾 |
DriveExists方法 | 判斷磁碟機是否存在 |
FileExists 、FolderExists方法 | 判斷檔案或資料夾是否存在 |
GetDriveName方法 | 取得磁碟機名稱 |
GetExtensionName方法 | 取得檔案的副檔名 |
GetParentFolderName方法 | 取得上層資料夾名稱 |
MoveFile方法 | 搬移檔案 |
MoveFolder方法 | 搬移資料夾 |
對於文字檔的讀寫,我們可以使用 FSO 物件的子物件: TextStream ,下面是這個物件的屬性和方法
AtEndOfLine屬性 | 判斷是否是文字行的最後一個字元 |
AtEndOfStream屬性 | 判斷是否是文字檔內容的最後(結尾) |
OpenTextFile 方法. Close 方法 | 開啟/關閉文字檔 |
Read方法 | 讀取文字內容 |
ReadAll方法 | 讀取文字檔案內全部文字內容 |
ReadLine方法 | 逐行讀取 |
Write方法 | 寫入字串 |
Writeline方法 | 寫入一行字串 |
WriteBlankLines方法 | 寫入一行空字串 |
我們來做個FSO的語法的練習:
<%
mydir = request("dir") '取得路徑字串放入變數
realpath = server.mappath(mydir) '取得絕對路徑
set my_FSO=CreateObject("Scripting.FileSystemObject") '使用FSO物件
mydir = request("dir") '取得路徑字串放入變數
realpath = server.mappath(mydir) '取得絕對路徑
set my_FSO=CreateObject("Scripting.FileSystemObject") '使用FSO物件
set my_folder=my_FSO.getfolder(realpath) '取得資料夾路徑
set folder_content=my_folder.subfolders '取得這個資料夾內的所有子資料夾
set file_list=my_folder.files '取得這個資料夾的檔案清單
if my_FSO.FileExists(realpath & "/index.htm") then '如果這個檔案存在
response.redirect "index.htm"
else
response.write "資料夾中沒有首頁 index.htm 檔"
set folder_content=my_folder.subfolders '取得這個資料夾內的所有子資料夾
set file_list=my_folder.files '取得這個資料夾的檔案清單
if my_FSO.FileExists(realpath & "/index.htm") then '如果這個檔案存在
response.redirect "index.htm"
else
response.write "資料夾中沒有首頁 index.htm 檔"
response.write file_list
response.write folder_content
response.write my_folder
end if
end if
%>
response.write folder_content
response.write my_folder
end if
end if
%>