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

下面是代码:

<%

' 网站根目录
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
%>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值