filesystemobject <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>FileSystemObject組件應該執行個體</title>
</head>
<body>
<input name="B0" type="button" value="建立文字檔" onClick="window.location='test1.asp?type=crea'">
<input name="B1" type="button" value="讀取檔案內容" onClick="window.location='test1.asp?type=read'">
<input name="B2" type="button" value="添加檔案內容" onClick="window.location='test1.asp?type=add'">
<input name="B3" type="button" value="修改檔案內容" onClick="window.location='test1.asp?type=edit'">
<input name="B4" type="button" value="刪除檔案內容" onClick="window.location='test1.asp?type=dele'">
<%
read="michael.txt"
'擷取檔案真實路徑
read=LEFT(Server.MapPath(Request.ServerVariables("PATH_INFO")),InstrRev(Server.MapPath(Request.ServerVariables("PATH_INFO")),"\")) & read
'response.write "檔案路徑:" & read & "<br>" '輸出檔案真實路徑
%>
<hr>
<%if request.querystring("type")="crea" then%>
<form action="test1.asp?type=crea" method="post" name="form1">
請輸入檔案名稱 : <input name="T1" size="45">.txt<br>
請輸入檔案內容:<textarea name="D1" cols="50" rows="5"></textarea>
<input name="bu" type="Submit" value="提交">
</form>
<%end if%>
<%if request.querystring("type")="add" then%>
<form action="test1.asp?type=add" method="post" name="form1">
<textarea name="D1" cols="70" rows="5"></textarea>
<input name="bu" type="Submit" value="提交">
</form>
<%end if%>
<%
if request.querystring("type")="edit" then
Set fs=CreateObject("Scripting.FileSystemObject")
if fs.FileExists(server.mappath("michael.txt")) then
Set ts=fs.OpenTextFile(server.mappath("michael.txt"))
%>
<form action="test1.asp?type=add" method="post" name="form1">
<textarea name="D1" cols="70" rows="5"><%=ts.ReadLine%></textarea>
<input name="bu" type="Submit" value="提交">
</form>
<%
else
response.write "找不到此檔案"
end if
end if
%>
<%
if request.querystring("type")="crea" and request.form<>"" then '建立新的檔案
Set fs=CreateObject("Scripting.FileSystemObject")
if fs.FileExists(server.mappath(request.form("T1")& ".txt")) then '判斷檔案是否存在
response.write "<script>alert('該檔案已經存在');window.location='test1.asp?type=crea';</script>"
else
Set ts=fs.CreateTextFile(server.mappath(request.form("T1")& ".txt"),True)
ts.close
Set ts=fs.OpenTextFile(server.mappath(request.form("T1")& ".txt"),2,True)
ts.WriteLine(request.form("D1")) '寫入檔案內容 注:WriteLine是加入內容並換行,Write是加入內容不換行
ts.close
response.write "<script>alert('建立檔案成功');window.location='test1.asp?type=read';</script>"
end if
set fs=nothing
end if
if request.querystring("type")="read" then '讀取檔案內容
Set fs=CreateObject("Scripting.FileSystemObject")
if fs.FileExists(server.mappath("michael.txt")) then '判斷檔案是否存在
Set ts=fs.OpenTextFile(server.mappath("michael.txt"))
response.write "檔案內容: "
do until ts.AtEndOfStream '判斷是否讀到檔案最後一行
response.write ts.readLine & "<br>" '讀取檔案逐行輸出
loop
ts.close
else
response.write "找不到此檔案"
end if
set fs=nothing
end if
if request.querystring("type")="add" and request.form<>"" then '添加檔案內容
Set fs=CreateObject("Scripting.FileSystemObject")
if fs.Fi