ASP自动检测创建多级目录,fso与stream生成文件函数

王朝学院·作者佚名  2009-06-25
窄屏简体版  字體: |||超大  

<%

' 网站根目录

const global_default_root_path = "/"

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

' 作者: xiaoyuehen

' 日期: 2006-6-3

' 参数: strlocalpath : string, 创建路径, 必须为物理路径

' 返回: True/False

function create_directory(strlocalpath)

create_directory = false

dim strlocalfolder

dim strpath, tmppath, tmptpath

dim arrpathlist, intlevel

strlocalfolder = server.mappath(global_default_root_path)

if left(strlocalpath, len(strlocalfolder)) <> strlocalfolder then

exit function

end if

' 获得目录

strpath

= replace(strlocalpath, strlocalfolder, "")

if left(strpath, 1) = "\" then

strpath = right(strpath, len(strpath) - 1)

end if

dim objfolder

set objfolder = server.createobject("Scripting.FileSystemObject")

' 如果该目录已存在, 则返回 true

if objfolder.folderexists(strlocalpath) then

create_directory = true

exit function

end if

arrpathlist

= split(strpath, "\")

intlevel

= ubound(arrpathlist)

dim i

tmptpath = ""

for i = 0 to intlevel

tmptpath = tmptpath & arrpathlist(i) & "\"

tmppath = strlocalfolder & "\" & tmptpath

if not objfolder.folderexists(tmppath) then objfolder.createfolder tmppath

next

set objfolder = nothing

create_directory = true

end function

' 描述: fso 生成文件

' 作者: xiaoyuehen

' 日期: 2006-6-3

' 参数: strlocalpath : string, 创建路径, 必须为物理路径

' 返回: True/False

function fcreate_file(strfilename, byref strcontent)

fcreate_file = false

dim strpath

strpath = left(strfilename, instrrev(strfilename, "\", -1, 1))

'检测路径及文件名有效性

if not(create_directory(strpath)) then exit function

const forreading = 1, forwriting = 2, forappending = 8

dim fso, f

set fso = createobject("scripting.filesystemobject")

set f = fso.opentextfile(strfilename, forwriting, true, -2)

f.write strcontent

f.close

set fso = nothing

set f = nothing

fcreate_file = true

end function

' 描述: stream 生成文件

' 作者: xiaoyuehen

' 日期: 2006-6-3

' 参数: strfilename : string, 文件完整路径, 必须为物理路径

'

strcontent : string, 文本内容

' 返回: True/False

function screate_file(strfilename, byref strcontent)

screate_file = false

dim strpath

strpath = left(strfilename, instrrev(strfilename, "\", -1, 1))

'检测路径及文件名有效性

if not(create_directory(strpath)) then exit function

dim ado_stream

const forreading = 1, forwriting = 2

set ado_stream = server.createobject("adodb.stream")

with ado_stream

.Open

.type = 2

.Writetext(strcontent)

.SaveToFile strfilename, forwriting

.SetEOS

end with

set ado_stream = nothing

screate_file = true

end function%>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航