当前位置: 移动技术网 > IT编程>开发语言>Asp > FSO一些代码

FSO一些代码

2017年12月12日  | 移动技术网IT编程  | 我要评论
使用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 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 

fso替换指定文件的字符
程序代码:

'fso替换指定文件的字符
function fsolineedit(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
'response.write fsolineedit("test.txt","世界","明天是一个好天去")


删除文件
程序代码:

'删除文件
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(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写文件某一行的函数
程序代码:

'使用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 = jo& #105;n(temparray,chr(13)&chr(10))
set f = fso.cr& #101;atetextfile(server.mappath(filename),true)
f.write tempcnt
end if
f.close
set f = nothing
end function


建立目录的程序,如果有多级目录,则一级一级的创建
程序代码: 
'建立目录的程序,如果有多级目录,则一级一级的创建
function createdir(byval localpath)
on error resume next
localpath = replace(localpath,"\","/")
set fileobject = server.createobject("scripting.filesystemobject")
patharr = split(localpath,"/")
path_level = ubound(patharr)
for i = 0 to path_level
if i=0 then pathtmp=patharr(0) & "/" else pathtmp = pathtmp & patharr(i) & "/"
cpath = left(pathtmp,len(pathtmp)-1)
if not fileobject.folderexists(cpath) then fileobject.createfolder cpath
next
set fileobject = nothing
if err.number <> 0 then
createdir = false
err.clear
else
createdir = true
end if
end function


下面列举一下这些不常用但是却非常酷的功能: 
很少被了解的fso功能 
getspecialfolder method 返回特定的windows文件夹的路径: windows安装目录;windows系统目录;windows临时目录 fso.getspecialfolder([0, 1, or 2])  
gettempname method 返回一个随机产生的文件或者目录名字,用于需要存储临时数据时 
getabsolutepathname method 返回文件夹的绝对路径(类似于server.mappath)。 
比如,fso.getabsolutepathname("region") 将返回类似于下面的结果:"c:mydocsmyfolder egion" 
getextensionname method 返回路径中最后部分的扩展名 
(比如:fso.getextensionname("c:docs est.txt") 将返回txt) 
getbasename and getparentfolder methods 返回路径中最后部分的父文件夹 
(比如:fso.getparentfolder ("c:docsmydocs") 将返回'docs') 
drives property 返回所有本地可用驱动器的集合,用于建立资源浏览器样的用户接口。 

使用上面的功能时,最好建立好出错处理的代码。因为如果需要的参数不存在,将会产生麻烦的信息。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网