ASP应用之FSO组件之文件操作大全

then 
exit function 
else 
FSOlinedit = temparray(lineNum-1) 
end if 
end if 
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 chr(13)&chr(10)&Linecontent 
f.close 
set f = 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 filenum(filename)
Dim temparray
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
temparray = split(tempcnt,chr(13)&chr(10))
lineNum=ubound(temparray)
filenum =lineNum
f.close
End if
End Function 



动态include文件

'利用FSO动态调用文件
Function include(filename)
Dim re,Content,FSO,F,aspStart,aspEnd
Set FSO = CreateObject("Scripting.FileSystemObject")
Set F = FSO.OpenTextFile(Server.MapPath(filename))
Content = F.ReadAll
F.Close
Set = Nothing
Set FSO = Nothing
Set re = New RegExp
re.pattern = "s*="
aspEnd = 1
aspStart = inStr(aspEnd,content,"<%")+2
Do While aspStart>aspEnd+1
Response.Write Mid(content,aspEnd,aspStart-aspEnd-2)
aspEnd=inStr(aspStart,content,"%>")+2
Execute(re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write "))
aspStart=inStr(aspEnd,content,"<%")+2
Loop
Response.Write Mid(content,aspEnd)
Set re = Nothing
End Function

删除文件

Function DelFile(Filename)
If Filename <> "" Then
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(Filename) Then
FSO.DeleteFile Filename
End If
Set FSO = Nothing
End If
End Function

判断文件是否存在
Function ReportFileStatus(filespec)
Dim FSO,msg
Set FSO = CreateObject("Scripting.FileSystemObject")
If (FSO.FileExists(filespec)) Then
msg = filespec & " exists."
Else
msg = filespec & " doesn't exist."
End If
ReportFileStatus = msg
End Function

使用FSO修改文件特定内容的函数

'使用FSO修改文件特定内容的函数
Function FSOchange(filename,Target,String)
Dim objFSO,objCountFile,FiletempData
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objCountFile = objFSO.OpenTextFile(Serve

零度电脑_基础知识_最新消息