ASP应用之FSO组件之文件操作大全
t;%
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。更精彩的依然是在后面。
FSO从TXT循环读取文本
<%
whichfile=server.mappath("info.txt")
Set fso = CreateObject("Scripting.FileSystemObject")
Set txt = fso.OpenTextFile(whichfile,1)
%>
<% do while not txt.atendofstream
rline = txt.ReadLine %>
<% Response.Write rline %>
<%
loop
txt.Close
%>
FSO生成文本文件
<%
dim str
str="Hnumber"
path=Server.MapPath(str&".txt")
set fso=server.createobject("scripting.filesystemobject")
If fso.FileExists(path) = True Then
fso.DeleteFile(path)
End If
set fs=fso.createtextfile(Server.MapPath(str&".txt"),true)'生成文件
'FSO写入文本文件
Number=100
fs.writeline(Number)
'FSO读取文本文件数据
whichfile=server.mappath(str&".txt")
Set fso = CreateObject("Scripting.FileSystemObject")
Set txt = fso.OpenTextFile(whichfile,1)
rline = txt.ReadLine
rline = rline & "<br>" & txt.ReadLine '读取第2行(可以省略,如果你觉得有第2行就读取)
Response.Write rline
txt.Close
'最后不要忘了关闭FS对象哦 ^_^
set fs=nothing
%>
使用FSO修改文件特定内容的函数
function FSOchange(filename,Target,String)
Dim objFSO,objCountFile,FiletempData
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objCountFile = objFSO.OpenTextFile(Server.MapPath(filename),1,True)
FiletempData = objCountFile.ReadAll
objCountFile.Close
FiletempData=Replace(FiletempData,Target,String)
Set objCountFile=objFSO.CreateTextFile(Server.MapPath(filename),True)
objCountFile.Write FiletempData
objCountFile.Close
Set objCountFile=Nothing
Set objFSO = Nothing
End Function
使用FSO读取文件内容的函数
function FSOFileRead(filename)
Dim objFSO,objCountFile,FiletempData
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objCountFile = objFSO.OpenTextFile(Server.MapPath(filename),1,True)
FSOFileRead = objCountFile.ReadAll
objCountFile.Close
Set objCountFile=Nothing
Set objFSO = Nothing
End Function
使用FSO读取文件某一行的函数
function FSOlinedit(filename,lineNum)
if linenum < 1 then exit function
dim fso,f,temparray,tempcnt
set fso = server.CreateObject("scripting.filesystemobject")
if not fso.fileExists(server.mappath(filename)) then exit function
set f = fso.opentextfile(server.mappath(filename),1)
if not f.AtEndofStream then
tempcnt = f.readall
f.close
set f = nothing
temparray = split(tempcnt,chr(13)&chr(10))
if lineNum>ubound(temparray)+1
|