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

6/24/2009来源:ASP技巧人气:9301

<%

' 网站根目录
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%>