首页  ·  知识 ·  编程语言
用FSO修改文件特定内容的函数
单翼  http://blog.sjzj.com.cn/  ASP  编辑:dezai  图片来源:网络
FunctionFSOchange(filename,Target,String)DimobjFSO,objCountFile,FiletempDataSetobjFSO=Server.CreateObject("Scri
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 FSOlinewrite(filename,lineNum,Linecontent)
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
temparray = split(tempcnt,chr(13)&chr(10))
if lineNum>ubound(temparray)+1 then
exit function
else
temparray(lineNum-1) = lineContent
end if
tempcnt = join(temparray,chr(13)&chr(10))
set f = fso.createtextfile(server.mappath(filename),true)
f.write tempcnt
end if
f.close
set f = nothing
End Function
'使用FSO添加文件新行的函数
Function FSOappline(filename,Linecontent)
dim fso,f
set fso = server.CreateObject("scripting.filesystemobject")
if not fso.fileExists(server.mappath(filename)) then exit function
set f = fso.opentextfile(server.mappath(filename),8,1)
f.write Linecontent&chr(13)&chr(10)
f.close
set f = 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 then
exit function
else
FSOlinedit = temparray(lineNum-1)
end if
end if
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
'检查文件内容是否空的函数
Function FSOFilenell(filename)
dim myfso,myread
set myfso=createobject("scripting.filesystemobject")
set myread=myfso.opentextfile(server.mappath(filename),1,0) 
if myread.atendofstream then
FSOFilenell=0
else
FSOFilenell=1
end if
myread.Close
Set myread=Nothing
Set myfso = Nothing
End Function
'读文件最后一行的函数
Function FSOlastline(filename)
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))
FSOlastline = temparray(ubound(temparray))
end if
End Function
'生成文件
Function FSOcreate(filename)
dim fso,f
set fso = server.CreateObject("scripting.filesystemobject")
set f = fso.CreateTextFile(server.mappath(filename),true)
set f = nothing
set fso = nothing
End Function
'检查是否有文件
Function FSOfile(filename)
dim fso,f
set fso = server.CreateObject("scripting.filesystemobject")
f=server.MapPath(filename)
if fso.FileExists(f) then
FSOfile=1
else
FSOfile=0
end if
set f = nothing
set fso = nothing
End Function
'删掉文件
Function FSOdel(filename)
dim fso,f
set fso = server.CreateObject("scripting.filesystemobject")
f=server.MapPath(filename)
if fso.FileExists(f) then
fso.DeleteFile f,true
end if
set f = nothing
set fso = nothing
End Function



本文作者:单翼 来源:http://blog.sjzj.com.cn/
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读