<%
whichfile=server.mappath("info.txt")
Set fso = CreateObject("Scripting.FileSystemObject")
Set txt = fso.OpenTextFile(whichfile,1)
rline = txt.ReadLine
rline = rline & "<br>" & txt.ReadLine
Response.Write rline
txt.Close
%>
需要注意:無論是通過FSO開啟磁碟機、開啟檔案夾、開啟檔案,以及以後要接觸到的開啟
資料庫,都只能是開啟絕對實體路徑地址。但一般情況是上傳到空間服務商那,不能很直接地
瞭解到自己檔案的所在位置,所以強烈推薦使用server.mappath方法:平台移植性強,適用
性強。
CreateObject("Scripting.FileSystemObject")建立了FSO組件的串連,
fso.OpenTextFile(whichfile,1)開啟了info.txt該檔案。參數“1”表示
“ForReading:以唯讀方式開啟檔案。不能寫這個檔案。”,其他還有參數
“2”表示“ForWriting:以寫方式開啟檔案”,參數“8”表示“ForAppending:
開啟檔案並從檔案末尾開始寫”。
開啟了該檔案,接下來是不是要顯示檔案中的內容?那就通過txt.ReadLine
方法讀取文本中的一整行,如果需要繼續讀取下一行,則繼續使用txt.ReadLine
方法。當然初此還有其它的讀取方法,比如txt.Read(7)讀取指定數量的字元,
txt.ReadAll返迴文本中的全部內容。
二、fso.CreateTextFile
如fso.CreateFolder建立檔案夾般,fso.CreateTextFile則是建立檔案了。
3,creattxt.asp
<%
whichfile=server.mappath("info.txt")
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile(whichfile,True)
MyFile.WriteLine("My Name Is CN-Bruce")
MyFile.WriteLine("My Sex Is Male")
MyFile.Close
%>
<a href="opentxt.asp">查看內容</a>
本次建立的檔案是上一info.txt檔案,fso.CreateTextFile(whichfile,True)
其中的參數true即表示能覆蓋已有檔案。建立後需要向裡面添加資料就採用
“MyFile.WriteLine”了。
那現在就可以建立一個簡單的文本記髁耍辜塹靡鄖暗募鞘浚?,
通過application、session、global.asa進行記數;2,通過Counter組件進行記數。
但兩者都有通病,就是不能儲存,如果伺服器重新啟動後,是不是所有記數全部清空了呢:)
那現在就可以使用文本來記錄資料了,即使重啟,下次提取的也還是該檔案。
實驗:文本計數器
首先建立一記數的文字檔counter.txt,設定初始值為“1”
4,counter.txt
1
接著是記數的ASP檔案,功能是顯示文本的記數,本做加1的記數,
然後還要將新的記數寫入文字檔。
5,txtcount.asp
<%
whichfile=server.mappath("counter.txt")
'開啟檔案並將其值讀取,最後關閉串連釋放資源
set fso=createobject("Scripting.FileSystemObject")
set openfile=fso.opentextfile(whichfile,1)
visitors=openfile.readline
openfile.close
'頁面顯示記數內容並做加1運算
response.write "您是本頁的第"&visitors&"位訪客"
visitors=visitors+1
'將新的數值添加寫入到文本,最後關閉所有串連釋放資源
set creatfile=fso.createtextfile(whichfile)
creatfile.writeLine(visitors)
creatfile.close
set fso=nothing
%>
那根據這個可以繼續地擴充內容:比如讓記數用數字圖片顯示。
當然前提就是你需要0-9的10張記數圖片,並將此圖片放於img檔案夾中。
下為一增強txtcount.asp內容代碼。
<%
whichfile=server.mappath("counter.txt")
set fso=createobject("Scripting.FileSystemObject")
set openfile=fso.opentextfile(whichfile,1)
visitors=openfile.readline
openfile.close
CountLen=len(visitors)
response.write "您是本頁的第"
for i=1 to 6-countLen '表示最大值999999
response.write "<img src=http://www.webjx.com/htmldata/2005-05-17/img/0.gif></img>"
next
for i=1 to countlen
response.write "<img src=img/" & mid(visitors,i,1) & ".gif></img>"
next
response.write "位訪客"
visitors=visitors+1
set creatfile=fso.createtextfile(whichfile)
creatfile.writeLine(visitors)
creatfile.close
set fso=nothing
%>
本程式中採用的是mid函數,該函數的作用是返回某字串中從第幾位
字元開始的幾個字元。格式即為:Mid(string,start,length)
<script language=vbs>
cn_string= "cnbruce love cnrose"
cn_start = 9
cn_length = 4
alert (mid(cn_string,cn_start,cn_length))
</script>
學會了FSO提取檔案值,也學會了將資訊輸入到檔案中,那下面就再來應用應用下。 不知道你有沒有這樣的習慣:看到一個檔案,不自覺的右鍵選擇用記事本開啟。
呵呵,幾乎沒有哪個檔案是不可以的。所以現在,可以預設所有檔案都是文本,
只是尾碼名不同而已;那麼也就是說,現在可以提取任一檔案的內容資訊。OK,就來想象一下:
1,提取一個檔案的路徑(採用file按鈕進行尋找定位)
2,將該路徑檔案開啟,並讀取所有行
3,顯示讀取的資訊
一、viewcode.asp
<%
Function ShowCode(filename)
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set cnrs = fso.OpenTextFile(filename, 1)
While Not cnrs.AtEndOfStream
rsline = cnrs.ReadLine
rsline = server.HTMLEncode(rsline)
Response.Write(rsline & "<br>")
Wend
end Function
%>
<form action="viewcode.asp" method="post">
輸入檔案名稱<input type="file" name="filename">
<input type="submit" value="查看來源程式">
</form>
<%
file=request.form("filename")
response.write (file & "來源程式如下<hr>")
If trim(file)<> "" then
Call ShowCode(file)
End If
%>
以上程式調試時,可以選擇html,asp頁面,也可以開啟任一應用程式等。
定義的ShowCode函數,主要作用是開啟、讀取並顯示檔案中所有資訊內容。
注意添加了server.HTMLEncode(rsline),針對含有標準HTML代碼的檔案。
顯示檔案中所有行即用一條件迴圈進行遍曆顯示了。
While Not cnrs.AtEndOfStream
...
Wend
接著,下面的這個例題具體就涉及open方法的問題了,還記得?正常情況之
下開啟檔案是採用fso.OpenTextFile("c:\testfile.txt",1),參數1的作用是:
以唯讀模式開啟檔案。不能對此檔案進行寫操作。如果現在已經存在一檔案,
需要進行追加寫入,則該怎麼辦呢?簡單,參數為8即可。
這有什麼用呢?呵呵,亞瑪遜的網路故事接龍就是如此:能接龍就需要首先
要顯示原有故事,然後自己添加故事寫入檔案。這其中的寫入檔案最講究的就是
追加寫入了。所以下面就可以實現。
二、story.asp
<%
If not request.Form("NextLine")="" then
Set fso=Server.CreateObject("Scripting.FileSystemobject")
textfile1=Server.MapPath("story.txt")
set cnrs=fso.OpenTextFile(textfile1,8)
cnrs.WriteLine(Request.Form("NextLine"))
cnrs.Close
end if
%>
故事如下:
<%
Set fso=Server.CreateObject("Scripting.FileSystemObject")
textfile1=Server.MapPath("story.txt")
set cnrs=fso.OpenTextFile(textfile1,1)
while not cnrs.AtEndOfStream
Response.Write " " & cnrs.ReadLine
wend
cnrs.close
%>
<hr>
<form method="post" action="story.asp">
請輸入這個故事的新行:<input name="NextLine" type="text" size="70">
<input type="submit" value="提交">
</form>
整個就是一很簡單的讀取資訊和加入資訊的混合利用,相信有了前面的基礎
看懂應該不成問題。當然還缺少個story.txt檔案,裡面寫好故事開頭就可以了。
調試
再下面,繼續來,該側重點主要就是練習一些函數的提示了。
1,instr函數:返回某字串在另一字串中第一次出現的位置。
比如現在尋找字母“A”在字串“A110B121C119D1861”中第一次出現的位置,則可以
<script language=vbs>
my_string = "A110B121C119D1861"
a_num = instr(my_string,"A")
alert(a_num)
</script>
同樣字母“B”的位置也就能確定。現在就來最關鍵的:提前字母“A”和“B”
中間的值“110”。還記得mid函數嗎?mid函數的主要作用是:從字串中返回指定數目的字元。
比如現在的“110”則應該是從字串的第2位取得3個單位的值。
<script language=vbs>
my_string = "A110B121C119D1861"
a_value = mid(my_string,2,3)
alert(a_value)
</script>
但設想一下:如果不是“110”,而是“1100”,那是不是要提取4位……這樣就
顯出程式的不夠完美。
所以繼續思考:所提取的值,永遠是在字母“A”後面的,且值也永遠是在字母
“A”和“B”之間的,
那麼只要分別提取出“A”、“B”的位置,則中間數值的起始位應是字母“A”位+1,
中間數值的長度應
是字母“B”位-字母“A”位-1
那麼現在就可以讓程式完美起來:
<script language=vbs>
my_string = "A110B121C119D1861"
a_num = instr(my_string,"A")
b_num = instr(my_string,"B")
a_value = mid(my_string,a_num+1,b_num-a_num-1)
alert(a_value)
</script>
OK,那麼現在你也就完全可以把字母“B”、“C”、“D”後面的值一一提取了。
當然需要注意的就是“D”後面有幾位怎麼取呢?採用字串總長度-字母D所在
位置數就可以了。
<script language=vbs>
my_string = "A110B121C119D1861"
a_num = instr(my_string,"A")
b_num = instr(my_string,"B")
c_num = instr(my_string,"C")
d_num = instr(my_string,"D")
total_num = len(my_string)
a_value = mid(my_string,a_num+1,b_num-a_num-1)
b_value = mid(my_string,b_num+1,c_num-b_num-1)
c_value = mid(my_string,c_num+1,d_num-c_num-1)
d_value = mid(my_string,d_num+1,total_num-d_num)
alert(a_value)
alert(b_value)
alert(c_value)
alert(d_value)
</script>
用到現在,你也許確實學到了不少,但也許會提出疑問:這個放在FSO檔案操作
裡有什麼作用呢?
那下面才是我們的正題:用FSO進行簡單的文本投票。
投票頁面首要的就是顯示各類項目的投票數,並相應賦於某個變數。然後
判斷本次投票的選相,
相對應地將投票數值加1,完畢後再將所有值繼續寫入文本。
1,一個HTML表單頁website.html
以做投票點擊的平台。
用到現在,你也許確實學到了不少,但也許會提出疑問:這個放在FSO檔案操作裡
有什麼作用呢?
那下面才是我們的正題:用FSO進行簡單的文本投票。
<form action="result.asp" method="post">
<input type="radio" name="website" value="A" checked> cnbruce.com<br>
<input type="radio" name="website" value="B"> blueidea.com<br>
<input type="radio" name="website" value="C"> it365cn.com<br>
<input type="radio" name="website" value="D"> 5d.cn<br>
<input type="submit">
<input type="reset">
2,接受表單頁值的result.asp
<%
whichfile=server.mappath("site.txt")
set fso=createobject("Scripting.FileSystemObject")
set thisfile=fso.opentextfile(whichfile)
my_string=thisfile.readline
a_num = instr(my_string,"A")
b_num = instr(my_string,"B")
c_num = instr(my_string,"C")
d_num = instr(my_string,"D")
total_num = len(my_string)
a_value = mid(my_string,a_num+1,b_num-a_num-1)
b_value = mid(my_string,b_num+1,c_num-b_num-1)
c_value = mid(my_string,c_num+1,d_num-c_num-1)
d_value = mid(my_string,d_num+1,total_num-d_num)
select case request.form("website")
case "A": a_value=a_value+1
case "B": b_value=b_value+1
case "C": c_value=c_value+1
case "D": d_value=d_value+1
end select
mynew_string="A" & cstr(a_value) & "B" & cstr(b_value) & "C"
& cstr(c_value) & "D" & cstr(d_value)
set newfile=fso.createtextfile(whichfile)
newfile.writeLine(mynew_string)
newfile.close
set fso=nothing
%>
當前投票:<br>
cnbruce.com:<%=a_value%><br>
blueidea.com:<%=b_value%><br>
it356cn.com:<%=c_value%><br>
5d.cn:<%=d_value%><br>
<a href="website.html">返回繼續</a>
有了上面函數的基礎,看這個應該不是很難的。
3,最後不要忘了的記數檔案site.txt
格式:A1B1C1D1
調試
OK,三個檔案就可以勝任一個很簡單的投票系統了,如果要加強,
需要細化的則結合以前的知識吧,比如投過一次後設定session或者cookies,
當再次投票時候判斷如果session或者cookies存在則不允許,也就是簡單的投票防作
假手段了……當然更多的還是要自己去想去實踐了。
一,fso.GetFile
提取檔案相應的 File 對象
1,getfile.asp
<%
whichfile=Server.MapPath("cnbruce.txt")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile(whichfile,true)
f1.Write ("This is a test.My Name is cnbruce.")
f1.Close
Set f2 = fso.GetFile(whichfile)
s = "檔案名稱:" & f2.name & "<br>"
s = s & "檔案短路徑名:" & f2.shortPath & "<br>"
s = s & "檔案物理地址:" & f2.Path & "<br>"
s = s & "檔案屬性:" & f2.Attributes & "<br>"
s = s & "檔案大小: " & f2.size & "<br>"
s = s & "檔案類型: " & f2.type & "<br>"
s = s & "檔案建立時間: " & f2.DateCreated & "<br>"
s = s & "最近訪問時間: " & f2.DateLastAccessed & "<br>"
s = s & "最近修改時間: " & f2.DateLastModified
response.write(s)
%>
其效果正如右鍵某檔案,看到的具體屬性資訊。
其中Attributes返回的數值“32”表示:(Archive)上次備份後已更改的檔案。可讀寫。
其它值附錄如下:
Normal 0 普通檔案。 沒有設定任何屬性。
ReadOnly 1 唯讀檔案。 可讀寫。
Hidden 2 隱藏檔案。 可讀寫。
System 4 系統檔案。 可讀寫。
Directory 16 檔案夾或目錄。 唯讀。
Archive 32 上次備份後已更改的檔案。 可讀寫。
Alias 1024 連結或捷徑。 唯讀。
Compressed 2048 壓縮檔。 唯讀。
二,file.move
作用將指定的檔案或檔案夾從某位置移動到另一位置。其實該方法仍然屬於
fso.GetFile後的一個應用。
2,movefile.asp
<%
whichfile=Server.MapPath("cnbruce.txt")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile(whichfile,true)
f1.Write ("This is a test.My Name is cnbruce.")
f1.Close
Set f2 = fso.GetFile(whichfile)
f2.Move "C:\"
%>
<a href="C:\">查看下有沒有</a>
簡單的剪下粘貼的功能實現。
三,File.Copy
同樣屬於fso.GetFile後的一個應用。就只是單純地拷貝檔案到某位置。
3,copyfile.asp
<%
whichfile=Server.MapPath("cnbruce.txt")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile(whichfile,true)
f1.Write ("This is a test.My Name is cnbruce.")
f1.Close
Set f2 = fso.GetFile(whichfile)
f2.Copy "D:\"
%>
<a href="D:\">查看下有沒有</a>
和本ASP頁面同在目錄下的cnbruce.txt檔案依然存在。
四,file.Delete
很顯然,就是直接刪除檔案了。
4,delfile.asp
<%
whichfile=Server.MapPath("cnbruce.txt")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile(whichfile,true)
f1.Write ("This is a test.My Name is cnbruce.")
f1.Close
Set f2 = fso.GetFile(whichfile)
f2.move "d:\"
Set f3 = fso.GetFile("d:\cnbruce.txt")
f3.delete
%>
<a href="d:\">查看下是沒有該檔案的</a>
當然FSO還沒有結束,比如上傳檔案,ASP轉HTML等都需要用到FSO。更精彩的依然是在後面。