手机
当前位置:查字典教程网 >编程开发 >ASP教程 >FSO一些代码
FSO一些代码
摘要:使用FSO修改文件特定内容的函数functionFSOchange(filename,Target,String)DimobjFSO,obj...

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

functionFSOchange(filename,Target,String)

DimobjFSO,objCountFile,FiletempData

SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")

SetobjCountFile=objFSO.OpenTextFile(Server.MapPath(filename),1,True)

FiletempData=objCountFile.ReadAll

objCountFile.Close

FiletempData=Replace(FiletempData,Target,String)

SetobjCountFile=objFSO.CreateTextFile(Server.MapPath(filename),True)

objCountFile.WriteFiletempData

objCountFile.Close

SetobjCountFile=Nothing

SetobjFSO=Nothing

EndFunction

使用FSO读取文件内容的函数

functionFSOFileRead(filename)

DimobjFSO,objCountFile,FiletempData

SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")

SetobjCountFile=objFSO.OpenTextFile(Server.MapPath(filename),1,True)

FSOFileRead=objCountFile.ReadAll

objCountFile.Close

SetobjCountFile=Nothing

SetobjFSO=Nothing

EndFunction

使用FSO读取文件某一行的函数

functionFSOlinedit(filename,lineNum)

iflinenum<1thenexitfunction

dimfso,f,temparray,tempcnt

setfso=server.CreateObject("scripting.filesystemobject")

ifnotfso.fileExists(server.mappath(filename))thenexitfunction

setf=fso.opentextfile(server.mappath(filename),1)

ifnotf.AtEndofStreamthen

tempcnt=f.readall

f.close

setf=nothing

temparray=split(tempcnt,chr(13)&chr(10))

iflineNum>ubound(temparray)+1then

exitfunction

else

FSOlinedit=temparray(lineNum-1)

endif

endif

endfunction

使用FSO写文件某一行的函数

functionFSOlinewrite(filename,lineNum,Linecontent)

iflinenum<1thenexitfunction

dimfso,f,temparray,tempCnt

setfso=server.CreateObject("scripting.filesystemobject")

ifnotfso.fileExists(server.mappath(filename))thenexitfunction

setf=fso.opentextfile(server.mappath(filename),1)

ifnotf.AtEndofStreamthen

tempcnt=f.readall

f.close

temparray=split(tempcnt,chr(13)&chr(10))

iflineNum>ubound(temparray)+1then

exitfunction

else

temparray(lineNum-1)=lineContent

endif

tempcnt=join(temparray,chr(13)&chr(10))

setf=fso.createtextfile(server.mappath(filename),true)

f.writetempcnt

endif

f.close

setf=nothing

endfunction

使用FSO添加文件新行的函数

functionFSOappline(filename,Linecontent)

dimfso,f

setfso=server.CreateObject("scripting.filesystemobject")

ifnotfso.fileExists(server.mappath(filename))thenexitfunction

setf=fso.opentextfile(server.mappath(filename),8,1)

f.writechr(13)&chr(10)&Linecontent

f.close

setf=nothing

endfunction

读文件最后一行的函数

functionFSOlastline(filename)

dimfso,f,temparray,tempcnt

setfso=server.CreateObject("scripting.filesystemobject")

ifnotfso.fileExists(server.mappath(filename))thenexitfunction

setf=fso.opentextfile(server.mappath(filename),1)

ifnotf.AtEndofStreamthen

tempcnt=f.readall

f.close

setf=nothing

temparray=split(tempcnt,chr(13)&chr(10))

FSOlastline=temparray(ubound(temparray))

endif

endfunction

FSO替换指定文件的字符

程序代码:

'FSO替换指定文件的字符

FunctionFSOLineEdit(filename,Target,String)

DimobjFSO,objCountFile,FiletempData

SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")

SetobjCountFile=objFSO.OpenTextFile(Server.MapPath(filename),1,True)

FiletempData=objCountFile.ReadAll

objCountFile.Close

FiletempData=Replace(FiletempData,Target,String)

SetobjCountFile=objFSO.CreateTextFile(Server.MapPath(filename),True)

objCountFile.WriteFiletempData

objCountFile.Close

SetobjCountFile=Nothing

SetobjFSO=Nothing

EndFunction

'Response.WriteFSOLineEdit("test.txt","世界","明天是一个好天去")

删除文件

程序代码:

'删除文件

FunctionDelFile(Filename)

IfFilename<>""Then

SetFSO=Server.CreateObject("Scripting.FileSystemObject")

IfFSO.FileExists(Filename)Then

FSO.DeleteFileFilename

EndIf

SetFSO=Nothing

EndIf

EndFunction

判断文件是否存在

程序代码:

'判断文件是否存在

FunctionReportFileStatus(filespec)

DimFSO,msg

SetFSO=CreateObject("Scripting.FileSystemObject")

If(FSO.FileExists(filespec))Then

msg=filespec&"exists."

Else

msg=filespec&"doesn'texist."

EndIf

ReportFileStatus=msg

EndFunction

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

程序代码:

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

FunctionFSOchange(filename,Target,String)

DimobjFSO,objCountFile,FiletempData

SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")

SetobjCountFile=objFSO.OpenTextFile(Server.MapPath(filename),1,True)

FiletempData=objCountFile.ReadAll

objCountFile.Close

FiletempData=Replace(FiletempData,Target,String)

SetobjCountFile=objFSO.CreateTextFile(Server.MapPath(filename),True)

objCountFile.WriteFiletempData

objCountFile.Close

SetobjCountFile=Nothing

SetobjFSO=Nothing

EndFunction

使用FSO写文件某一行的函数

程序代码:

'使用FSO写文件某一行的函数

FunctionFSOlinewrite(filename,lineNum,Linecontent)

Iflinenum<1ThenExitFunction

DimFSO,f,temparray,tempCnt

SetFSO=Server.CreateObject("Scripting.FileSystemObject")

IfNotFSO.FileExists(Server.MapPath(filename))ThenExitFunction

Setf=FSO.OpenTextFile(Server.MapPath(filename),1)

IfNotf.AtEndofStreamThen

tempcnt=f.ReadAll

f.Close

temparray=Split(tempcnt,Chr(13)&Chr(10))

IflineNum>UBound(temparray)+1Then

ExitFunction

Else

temparray(lineNum-1)=lineContent

EndIf

tempcnt=join(temparray,Chr(13)&Chr(10))

Setf=FSO.createtextfile(Server.MapPath(filename),true)

f.Writetempcnt

EndIf

f.Close

Setf=Nothing

EndFunction

建立目录的程序,如果有多级目录,则一级一级的创建

程序代码:

'建立目录的程序,如果有多级目录,则一级一级的创建

FunctionCreateDIR(ByValLocalPath)

OnErrorResumeNext

LocalPath=Replace(LocalPath,"","/")

SetFileObject=Server.CreateObject("Scripting.FileSystemObject")

patharr=Split(LocalPath,"/")

path_level=UBound(patharr)

Fori=0topath_level

Ifi=0Thenpathtmp=patharr(0)&"/"Elsepathtmp=pathtmp&patharr(i)&"/"

cpath=Left(pathtmp,Len(pathtmp)-1)

IfNotFileObject.FolderExists(cpath)ThenFileObject.CreateFoldercpath

Next

SetFileObject=Nothing

IfErr.Number<>0Then

CreateDIR=False

Err.Clear

Else

CreateDIR=True

EndIf

EndFunction

下面列举一下这些不常用但是却非常酷的功能:

很少被了解的FSO功能

GetSpecialFolderMethod返回特定的Windows文件夹的路径:Windows安装目录;Windows系统目录;Windows临时目录FSO.GetSpecialFolder([0,1,or2])

GetTempNameMethod返回一个随机产生的文件或者目录名字,用于需要存储临时数据时

GetAbsolutePathNameMethod返回文件夹的绝对路径(类似于Server.MapPath)。

比如,FSO.GetAbsolutePathName("region")将返回类似于下面的结果:"c:mydocsmyfolderegion"

GetExtensionNameMethod返回路径中最后部分的扩展名

(比如:FSO.GetExtensionName("c:docsest.txt")将返回txt)

GetBaseNameandGetParentFolderMethods返回路径中最后部分的父文件夹

(比如:FSO.GetParentFolder("c:docsmydocs")将返回'docs')

DrivesProperty返回所有本地可用驱动器的集合,用于建立资源浏览器样的用户接口。

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

【FSO一些代码】相关文章:

ASP语法高亮类代码

asp获取当前URL代码

asp下载防盗链代码

一搜图片搜索小偷

sql语句的一些集合

ASP中一个字符串处理类

保证asp叶面一定不会出现乱码 UTF8

一段采集程序代码

ASP 环境下 VBS 事件应用 示例代码

asp 查询数据代码

精品推荐
分类导航