ASP FSO文件操作函数代码(复制文件、重命名文件、删除文件、替换字符串)
程序员文章站
2022-07-01 23:09:19
fso文件(file)对象属性 datecreated 返回该文件夹的创建日期和时间 datelastaccessed 返回最后一次访问该文件的日期和时间 datelast...
fso文件(file)对象属性
datecreated 返回该文件夹的创建日期和时间
datelastaccessed 返回最后一次访问该文件的日期和时间
datelastmodified 返回最后一次修改该文件的日期和时间
drive 返回该文件所在的驱动器的drive对象
name 设定或返回文件的名字
parentfolder 返回该文件的父文件夹的folder对象
path 返回文件的绝对路径,可使用长文件名
shortname 返回dos风格的8.3形式的文件名
shortpath 返回dos风格的8.3形式的文件绝对路径
size 返回该文件的大小(字节)
type 如果可能,返回一个文件类型的说明字符串
fso文件(file)对象方法
fso文件对象方法 用途
copyfile 拷贝一个或者多个文件到新路径
createtextfile 创建文件并且返回一个textstream对象
deletefile 删除一个文件
opentextfile 打开文件并且返回textstream对象,以便读取或者追加
重命名文件:
function rename(sourcename,destname)
dim ofso,ofile
set ofso=server.createobject("scripting.filesystemobject")
set ofile=ofso.getfile(server.mappath(sourcename))
ofile.name=destname
set ofso=nothing
set ofile=nothing
end function
删除文件:
function fsodel(filename)
dim fso,f
set fso = server.createobject("scripting.filesystemobject")
f=server.mappath(filename)
if fso.fileexists(f) then
fso.deletefile f,true
end if
set f = nothing
set fso = nothing
end function
替换文件中的字符串:
function fsoreplace(filename,target,repstring)
dim objfso,objcountfile,filetempdata
set objfso = server.createobject("scripting.filesystemobject")
set objcountfile = objfso.opentextfile(server.mappath(filename),1,true)
filetempdata = objcountfile.readall
objcountfile.close
filetempdata=replace(filetempdata,target,repstring)
set objcountfile=objfso.createtextfile(server.mappath(filename),true)
objcountfile.write filetempdata
objcountfile.close
set objcountfile=nothing
set objfso = nothing
end function
<%
'*******************************************************
'函数名:createfolder(spath)
'作 用: 创建目录
'参 数:spath : 创建的相对目录路径
'返回值:成功 true , 失败 false
'*******************************************************
'response.write createfolder("/dgsunshine/uploadfile/demo1/")
function createfolder(spath)
on error resume next
dim fso,arrfolder,folder,i,j
if spath="" then
createfolder = false
exit function
end if
if left(spath,1) = "/" then
folder = "/"
spath = mid(spath,2,len(spath))
else
folder = "./"
end if
if right(spath,1) = "/" then spath = left(spath,len(spath)-1)
arrfolder = split(spath,"/")
set fso = server.createobject("scripting.filesystemobject")
for i = 0 to ubound(arrfolder)
if i = 0 then
folder = folder & arrfolder(i) & "/"
else
folder = folder & arrfolder(i) & "/"
end if
if fso.folderexists(server.mappath(folder)) = false then
response.write server.mappath(folder)
fso.createfolder(server.mappath(folder))
end if
next
set fso = nothing
if err.number <> 0 then
err.clear()
createfolder = false
else
createfolder = true
end if
end function
function getfile(paramfilepath)
set fso = server.createobject("scripting.filesystemobject")
set fso_read = fso.opentextfile(server.mappath(paramfilepath),1,false,-2)
getfile = fso_read.readall
set fso_read = nothing
set fso = nothing
end function
'*******************************************************
'函数名:createfile(paramfilecontent,paramfilepath)
'作 用: 创建文件
'参 数:paramfilecontent '文件的内容
' paramfilepath '文件名(不包括路径)
'返回值:成功 true , 失败 false
'*******************************************************
function createfile(paramfilecontent,paramfilepath)
on error resume next
dim fso,fwrite
set fso = server.createobject("scripting.filesystemobject")
set fwrite = fso.createtextfile(server.mappath(paramfilepath),true)
fwrite.write paramfilecontent
fwrite.close()
set fwrite = nothing
set fso = nothing
if err.number <> 0 then
err.clear()
createfile = false
else
createfile = true
end if
end function
'*******************************************************
'函数名:delfile(filepath)
'作 用: 删除文件
'参 数:filepath '文件路径 多个文件用"|"隔开
'返回值:成功 true , 失败 false
'*******************************************************
function delfile(filepath)
on error resume next
dim fso,arrfile,i
if getsafestr(filepath,"")="" then
createfolder = false
exit function
end if
arrfile = split(filepath,"|")
set fso = server.createobject("scripting.filesystemobject")
for i=0 to ubound(arrfile)
filepath = arrfile(i)
if fso.fileexists(server.mappath(filepath)) then
fso.deletefile server.mappath(filepath)
end if
next
set fso = nothing
if err then
err.clear()
delfile = false
else
delfile = true
end if
end function
'*******************************************************
'函数名:delfolder(folderpath)
'作 用: 删除目录
'参 数:folderpath '目录路径 '多个目录用"|"分隔
'返回值:成功 true , 失败 false
'*******************************************************
function delfolder(folderpath)
on error resume next
dim fso,arrfolder,i
if getsafestr(folderpath,"")="" then
delfolder = false
exit function
end if
arrfolder = split(folderpath,"|")
set fso = server.createobject("scripting.filesystemobject")
for i=0 to ubound(arrfolder)
folderpath = arrfolder(i)
if fso.folderexists(server.mappath(folderpath)) then
fso.deletefolder server.mappath(folderpath)
end if
next
if err then
err.clear()
delfolder = false
'showerror "删除目录失败",""
else
delfolder = true
end if
end function
'*******************************************************
'函数名:isexistfile(filepath)
'作 用: 判断文件或目录是否存在
'参 数:filepath '文件路径 多个文件用"|"隔开
'返回值:成功 true , 失败 false
'*******************************************************
function isexistfile(filepath)
on error resume next
dim fso,arrfile,i
if getsafestr(filepath,"")="" then
isexistfile = false
end if
arrfile = split(filepath,"|")
set fso = server.createobject("scripting.filesystemobject")
for i=0 to ubound(arrfile)
filepath = arrfile(i)
if fso.fileexists(server.mappath(filepath)) then
isexistfile = true
end if
if fso.folderexists(server.mappath(filepath)) then
isexistfile = true
end if
next
set fso = nothing
if err then
err.clear()
isexistfile = false
'showerror "判断文件或目录是否存在失败",""
else
isexistfile = true
end if
end function
'*******************************************************
'函数名:delfile(filepath)
'作 用: 删除文件或目录
'参 数:filepath '文件路径 多个文件用"|"隔开
'返回值:成功 true , 失败 false
'*******************************************************
function delfile(filepath)
on error resume next
dim fso,arrfile,i
if getsafestr(filepath,"")="" then
createfolder = false
end if
arrfile = split(filepath,"|")
set fso = server.createobject("scripting.filesystemobject")
for i=0 to ubound(arrfile)
filepath = arrfile(i)
if fso.fileexists(server.mappath(filepath)) then
fso.deletefile server.mappath(filepath)
end if
if fso.folderexists(server.mappath(filepath)) then
fso.deletefolder server.mappath(filepath)
end if
next
set fso = nothing
if err then
err.clear()
delfile = false
'showerror "删除文件或目录失败",""
else
delfile = true
end if
end function
'*******************************************************
'函数名:renamefile((oldname,newname)
'作 用: 重命名文件或目录
'参 数:stroldname '原文件名 多个用"|"隔开
' strnewname '新文件名 多个用"|"隔开
' 上面两个参数请保持一致
'返回值:成功 true , 失败 false
'*******************************************************
function renamefile(stroldname,strnewname)
on error resume next
dim fso,arrold,arrnew,i,oldname,newname
old = getsafestr(stroldname,"")
newfile = getsafestr(strnewname,"")
if old ="" or newfile = "" then
renamefile = false
exit function
end if
arrold = split(stroldname,"|")
arrnew = split(strnewname,"|")
if ubound(arrold)<> ubound(arrnew) then
renamefile = false
exit function
end if
set fso = server.createobject("scripting.filesystemobject")
for i=0 to ubound(arrold)
oldname = server.mappath(arrold(i))
newname = server.mappath(arrnew(i))
if fso.fileexists(oldname) and not fso.fileexists(newname) then
fso.movefile oldname,newname
'renamefile = true
end if
next
set fso = nothing
if err.number <> 0 then
err.clear()
renamefile = false
else
renamefile = true
end if
end function
'*******************************************************
'函数名:copyfiles((tempsource,tempend)
'作 用: 复制文件或者目录
'参 数:tempsource '源文件名 多个用"|"隔开
' tempend '目的文件名 多个用"|"隔开
' 注意:上面两个参数请保持一致,并且都为完整路径,
' 已经经过server.mappath方法处理过
'返回值:成功 true , 失败 false
'*******************************************************
function copyfiles(tempsource,tempend)
on error resume next
dim copyfso,arrsource,arrend
copyfiles = false
set copyfso = server.createobject("scripting.filesystemobject")
if tempsource ="" or tempend = "" then
errraise "复制文件或目录","条件为空"
copyfiles = false
exit function
end if
arrsource = split(tempsource,"|")
arrend = split(tempend,"|")
if ubound(arrsource) <> ubound(arrend) then
copyfiles= false
exit function
end if
for i=0 to ubound(arrsource)
srcname = arrsource(i)
tarname = arrend(i)
if copyfso.fileexists(srcname) and not copyfso.fileexists(tarname) then
copyfso.copyfile srcname,tarname
copyfiles = true
end if
if copyfso.folderexists(srcname) and not copyfso.folderexists(tarname)then
copyfso.copyfolder srcname,tarname
copyfiles = true
end if
next
set copyfso = nothing
if err then
'err.clear()
copyfiles = false
end if
end function
%>
datecreated 返回该文件夹的创建日期和时间
datelastaccessed 返回最后一次访问该文件的日期和时间
datelastmodified 返回最后一次修改该文件的日期和时间
drive 返回该文件所在的驱动器的drive对象
name 设定或返回文件的名字
parentfolder 返回该文件的父文件夹的folder对象
path 返回文件的绝对路径,可使用长文件名
shortname 返回dos风格的8.3形式的文件名
shortpath 返回dos风格的8.3形式的文件绝对路径
size 返回该文件的大小(字节)
type 如果可能,返回一个文件类型的说明字符串
fso文件(file)对象方法
fso文件对象方法 用途
copyfile 拷贝一个或者多个文件到新路径
createtextfile 创建文件并且返回一个textstream对象
deletefile 删除一个文件
opentextfile 打开文件并且返回textstream对象,以便读取或者追加
重命名文件:
复制代码 代码如下:
function rename(sourcename,destname)
dim ofso,ofile
set ofso=server.createobject("scripting.filesystemobject")
set ofile=ofso.getfile(server.mappath(sourcename))
ofile.name=destname
set ofso=nothing
set ofile=nothing
end function
删除文件:
复制代码 代码如下:
function fsodel(filename)
dim fso,f
set fso = server.createobject("scripting.filesystemobject")
f=server.mappath(filename)
if fso.fileexists(f) then
fso.deletefile f,true
end if
set f = nothing
set fso = nothing
end function
替换文件中的字符串:
复制代码 代码如下:
function fsoreplace(filename,target,repstring)
dim objfso,objcountfile,filetempdata
set objfso = server.createobject("scripting.filesystemobject")
set objcountfile = objfso.opentextfile(server.mappath(filename),1,true)
filetempdata = objcountfile.readall
objcountfile.close
filetempdata=replace(filetempdata,target,repstring)
set objcountfile=objfso.createtextfile(server.mappath(filename),true)
objcountfile.write filetempdata
objcountfile.close
set objcountfile=nothing
set objfso = nothing
end function
<%
'*******************************************************
'函数名:createfolder(spath)
'作 用: 创建目录
'参 数:spath : 创建的相对目录路径
'返回值:成功 true , 失败 false
'*******************************************************
'response.write createfolder("/dgsunshine/uploadfile/demo1/")
function createfolder(spath)
on error resume next
dim fso,arrfolder,folder,i,j
if spath="" then
createfolder = false
exit function
end if
if left(spath,1) = "/" then
folder = "/"
spath = mid(spath,2,len(spath))
else
folder = "./"
end if
if right(spath,1) = "/" then spath = left(spath,len(spath)-1)
arrfolder = split(spath,"/")
set fso = server.createobject("scripting.filesystemobject")
for i = 0 to ubound(arrfolder)
if i = 0 then
folder = folder & arrfolder(i) & "/"
else
folder = folder & arrfolder(i) & "/"
end if
if fso.folderexists(server.mappath(folder)) = false then
response.write server.mappath(folder)
fso.createfolder(server.mappath(folder))
end if
next
set fso = nothing
if err.number <> 0 then
err.clear()
createfolder = false
else
createfolder = true
end if
end function
function getfile(paramfilepath)
set fso = server.createobject("scripting.filesystemobject")
set fso_read = fso.opentextfile(server.mappath(paramfilepath),1,false,-2)
getfile = fso_read.readall
set fso_read = nothing
set fso = nothing
end function
'*******************************************************
'函数名:createfile(paramfilecontent,paramfilepath)
'作 用: 创建文件
'参 数:paramfilecontent '文件的内容
' paramfilepath '文件名(不包括路径)
'返回值:成功 true , 失败 false
'*******************************************************
function createfile(paramfilecontent,paramfilepath)
on error resume next
dim fso,fwrite
set fso = server.createobject("scripting.filesystemobject")
set fwrite = fso.createtextfile(server.mappath(paramfilepath),true)
fwrite.write paramfilecontent
fwrite.close()
set fwrite = nothing
set fso = nothing
if err.number <> 0 then
err.clear()
createfile = false
else
createfile = true
end if
end function
'*******************************************************
'函数名:delfile(filepath)
'作 用: 删除文件
'参 数:filepath '文件路径 多个文件用"|"隔开
'返回值:成功 true , 失败 false
'*******************************************************
function delfile(filepath)
on error resume next
dim fso,arrfile,i
if getsafestr(filepath,"")="" then
createfolder = false
exit function
end if
arrfile = split(filepath,"|")
set fso = server.createobject("scripting.filesystemobject")
for i=0 to ubound(arrfile)
filepath = arrfile(i)
if fso.fileexists(server.mappath(filepath)) then
fso.deletefile server.mappath(filepath)
end if
next
set fso = nothing
if err then
err.clear()
delfile = false
else
delfile = true
end if
end function
'*******************************************************
'函数名:delfolder(folderpath)
'作 用: 删除目录
'参 数:folderpath '目录路径 '多个目录用"|"分隔
'返回值:成功 true , 失败 false
'*******************************************************
function delfolder(folderpath)
on error resume next
dim fso,arrfolder,i
if getsafestr(folderpath,"")="" then
delfolder = false
exit function
end if
arrfolder = split(folderpath,"|")
set fso = server.createobject("scripting.filesystemobject")
for i=0 to ubound(arrfolder)
folderpath = arrfolder(i)
if fso.folderexists(server.mappath(folderpath)) then
fso.deletefolder server.mappath(folderpath)
end if
next
if err then
err.clear()
delfolder = false
'showerror "删除目录失败",""
else
delfolder = true
end if
end function
'*******************************************************
'函数名:isexistfile(filepath)
'作 用: 判断文件或目录是否存在
'参 数:filepath '文件路径 多个文件用"|"隔开
'返回值:成功 true , 失败 false
'*******************************************************
function isexistfile(filepath)
on error resume next
dim fso,arrfile,i
if getsafestr(filepath,"")="" then
isexistfile = false
end if
arrfile = split(filepath,"|")
set fso = server.createobject("scripting.filesystemobject")
for i=0 to ubound(arrfile)
filepath = arrfile(i)
if fso.fileexists(server.mappath(filepath)) then
isexistfile = true
end if
if fso.folderexists(server.mappath(filepath)) then
isexistfile = true
end if
next
set fso = nothing
if err then
err.clear()
isexistfile = false
'showerror "判断文件或目录是否存在失败",""
else
isexistfile = true
end if
end function
'*******************************************************
'函数名:delfile(filepath)
'作 用: 删除文件或目录
'参 数:filepath '文件路径 多个文件用"|"隔开
'返回值:成功 true , 失败 false
'*******************************************************
function delfile(filepath)
on error resume next
dim fso,arrfile,i
if getsafestr(filepath,"")="" then
createfolder = false
end if
arrfile = split(filepath,"|")
set fso = server.createobject("scripting.filesystemobject")
for i=0 to ubound(arrfile)
filepath = arrfile(i)
if fso.fileexists(server.mappath(filepath)) then
fso.deletefile server.mappath(filepath)
end if
if fso.folderexists(server.mappath(filepath)) then
fso.deletefolder server.mappath(filepath)
end if
next
set fso = nothing
if err then
err.clear()
delfile = false
'showerror "删除文件或目录失败",""
else
delfile = true
end if
end function
'*******************************************************
'函数名:renamefile((oldname,newname)
'作 用: 重命名文件或目录
'参 数:stroldname '原文件名 多个用"|"隔开
' strnewname '新文件名 多个用"|"隔开
' 上面两个参数请保持一致
'返回值:成功 true , 失败 false
'*******************************************************
function renamefile(stroldname,strnewname)
on error resume next
dim fso,arrold,arrnew,i,oldname,newname
old = getsafestr(stroldname,"")
newfile = getsafestr(strnewname,"")
if old ="" or newfile = "" then
renamefile = false
exit function
end if
arrold = split(stroldname,"|")
arrnew = split(strnewname,"|")
if ubound(arrold)<> ubound(arrnew) then
renamefile = false
exit function
end if
set fso = server.createobject("scripting.filesystemobject")
for i=0 to ubound(arrold)
oldname = server.mappath(arrold(i))
newname = server.mappath(arrnew(i))
if fso.fileexists(oldname) and not fso.fileexists(newname) then
fso.movefile oldname,newname
'renamefile = true
end if
next
set fso = nothing
if err.number <> 0 then
err.clear()
renamefile = false
else
renamefile = true
end if
end function
'*******************************************************
'函数名:copyfiles((tempsource,tempend)
'作 用: 复制文件或者目录
'参 数:tempsource '源文件名 多个用"|"隔开
' tempend '目的文件名 多个用"|"隔开
' 注意:上面两个参数请保持一致,并且都为完整路径,
' 已经经过server.mappath方法处理过
'返回值:成功 true , 失败 false
'*******************************************************
function copyfiles(tempsource,tempend)
on error resume next
dim copyfso,arrsource,arrend
copyfiles = false
set copyfso = server.createobject("scripting.filesystemobject")
if tempsource ="" or tempend = "" then
errraise "复制文件或目录","条件为空"
copyfiles = false
exit function
end if
arrsource = split(tempsource,"|")
arrend = split(tempend,"|")
if ubound(arrsource) <> ubound(arrend) then
copyfiles= false
exit function
end if
for i=0 to ubound(arrsource)
srcname = arrsource(i)
tarname = arrend(i)
if copyfso.fileexists(srcname) and not copyfso.fileexists(tarname) then
copyfso.copyfile srcname,tarname
copyfiles = true
end if
if copyfso.folderexists(srcname) and not copyfso.folderexists(tarname)then
copyfso.copyfolder srcname,tarname
copyfiles = true
end if
next
set copyfso = nothing
if err then
'err.clear()
copyfiles = false
end if
end function
%>
推荐阅读
-
PHP操作文件类的函数代码(文件和文件夹创建,复制,移动和删除)
-
ASP FSO文件操作函数代码(复制文件、重命名文件、删除文件、替换字符串)
-
asp结合fso实现文件或文件夹创建删除等操作的函数
-
PHP操作文件类的函数代码(文件和文件夹创建,复制,移动和删除)
-
PHP操作文件类的函数代码(文件和文件夹创建,复制,移动和删除)_PHP教程
-
PHP操作文件类的函数代码(文件和文件夹创建,复制,移动和删除
-
PHP操作文件类的函数代码(文件和文件夹创建,复制,移动和删除_PHP
-
PHP操作文件类的函数代码(文件和文件夹创建,复制,移动和删除)_PHP教程
-
PHP操作文件类的函数代码(文件和文件夹创建,复制,移动和删除_PHP
-
ASP FSO文件操作函数代码(复制文件、重命名文件、删除文件、替换字符串)