欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp中文件与文件夹常用处理函数(文件后缀、创建文件等)

程序员文章站 2022-10-27 16:44:07
复制代码 代码如下:'===================================== '获得文件后缀 '============================...
复制代码 代码如下:

'=====================================
'获得文件后缀
'=====================================
function get_filetxt(byval t0)
dim t1
if len(t0)<2 or instr(t0,".")=0 then get_filetxt=false:exit function
t1=split(t0,".")
get_filetxt=lcase(t1(ubound(t1)))
end function

'=====================================
'读取任何文件的纯代码
'=====================================
function loadfile(byval t0)
if len(t0)=0 then exit function
if sdcms_cache then
if check_cache("loadfile_"&t0) then
create_cache "loadfile_"&t0,loadfile_cache(t0)
end if
loadfile=load_cache("loadfile_"&t0)
else
loadfile=loadfile_cache(t0)
end if
end function

function loadfile_cache(byval t0)
dim t1,stm
on error resume next
if len(t0)=0 then exit function
t1=empty
set stm=server.createobject("adodb.stream")
with stm
.type=2'以本模式读取
.mode=3
.charset=charset
.open
.loadfromfile server.mappath(t0)
t1=.readtext
.close
end with
set stm=nothing
if err then
loadfile_cache="“"&t0&"”"&err.description:err.clear
else
loadfile_cache=t1
end if
end function

'=====================================
'检查文件是否存在
'=====================================
function check_file(byval t0)
dim fso
t0=server.mappath(t0)
set fso=createobject("scripting.filesystemobject")
check_file=fso.fileexists(t0)
set fso=nothing
end function

'=====================================
'检查文件夹是否存在
'=====================================
function check_folder(byval t0)
dim fso
t0=server.mappath(t0)
set fso=createobject("scripting.filesystemobject")
check_folder=fso.folderexists(t0)
set fso=nothing
end function

'=====================================
'创建文件夹(无限级)
'=====================================
function create_upfile(byval t0)
dim t1,t2,objfso,i
on error resume next
t0=server.mappath(t0)
if instr(t0,"\")<=0 or instr(t0,":")<=0 then:create_upfile=false:exit function
set objfso=createobject("scripting.filesystemobject")
if objfso.folderexists(t0) then:create_upfile=true:exit function
t1=split(t0,"\"):t2=""
for i=0 to ubound(t1)
t2=t2&t1(i)&"\"
if not objfso.folderexists(t2) then objfso.createfolder(t2)
next
set objfso=nothing
if err=0 then create_upfile=true:else create_upfile=false:echo "create_upfile:"&err.description&"<br>":err.clear
end function

sub savefile(byval t0,byval t1,byval t2)
dim objfso,t3
set objfso=createobject("scripting.filesystemobject")
if t0="" then echo "目录不能为空!":died
t3=server.mappath(t0)
if t2="" or isnull(t2) then t2=""
if objfso.folderexists(t3)=false then create_upfile(t0)
buildfile t3&"\"&trim(t1),t2
set objfso=nothing
end sub

function buildfile(byval t0,byval t1)
dim stm
on error resume next
set stm=server.createobject("adodb.stream")
with stm
.type=2 '以本模式读取
.mode=3
.charset=charset
.open
.writetext t1
.savetofile t0,2
.close
end with
set stm=nothing
if err then echo "buildfile:"&err.description&"<br>":err.clear
end function

'=====================================
'重命名文件夹
'=====================================
sub renamefile(byval t0,byval t1)
dim fso
on error resume next
set fso=server.createobject("scripting.filesystemobject")
if fso.folderexists(server.mappath(t0)) then
fso.movefolder server.mappath(t0),server.mappath(t1)
end if
set fso=nothing
if err then echo "renamefile:"&err.description&"<br>":err.clear
end sub

'=====================================
'重命名文件
'=====================================
sub renamehtml(byval t0,byval t1)
dim fso
on error resume next
set fso=server.createobject("scripting.filesystemobject")
if fso.fileexists(server.mappath(t0)) then
fso.movefile server.mappath(t0),server.mappath(t1)
end if
set fso=nothing
if err then echo "renamehtml:"&err.description&"<br>":err.clear
end sub

'=====================================
'删除文件夹
'=====================================
sub delfile(byval t0)
dim fso,f
on error resume next
set fso=server.createobject("scripting.filesystemobject")
set f=fso.getfolder(server.mappath(t0))
if not isnull(t0) then f.delete true
if err then echo "delfile:"&err.description&"<br>":err.clear
end sub

'=====================================
'删除文件
'=====================================
sub delhtml(byval t0)
dim fso
on error resume next
set fso=server.createobject("scripting.filesystemobject")
if fso.fileexists(server.mappath(t0)) then fso.deletefile server.mappath(t0)
if err then echo "delhtml:"&err.description&"<br>":err.clear
end sub

function re_filename(byval t0)
dim t1
t0=lcase(t0)
if len(t0)=0 then re_filename="{id}":exit function
t1=now()
'处理自定义文件名

'if instr(t0,"{")>0 and instr(t0,"}")>0 then
'if instr(t0,"{id}")=0 then
't0=t0&"{id}"'尽量防止重复
'end if
'end if
t0=replace(t0,"{y}",year(t1))
t0=replace(t0,"{m}",right("0"&month(t1),2))
t0=replace(t0,"{d}",right("0"&day(t1),2))
t0=replace(t0,"{h}",right("0"&hour(t1),2))
t0=replace(t0,"{mm}",right("0"&minute(t1),2))
t0=replace(t0,"{s}",right("0"&second(t1),2))
re_filename=t0
end function