ASP常用文件操作函数

2023-10-11    分类: 网站建设

以下包含了常用的各种ASP常用文件操作函数:如创建一个指定的文件,建立文件目录,检查文件是否存在,删除文件目录,移动删除文件,读取文件内容等
<%
'创建一个指定的文件函数,可选参数,文件内容,文件目录与文件生成类型
Function createTextFile(Byval content,Byval fileDir,Byval code)
dim fileobj,fileCode : fileDir=replace(fileDir, "\", "/")
if isNul(code) then fileCode="gbk" else fileCode=code
call createfolder(fileDir,"filedir")
on error resume next:err.clear
set fileobj=objFso.CreateTextFile(server.mappath(fileDir),True)
fileobj.Write(content)
set fileobj=nothing
if Err or not isNul(code) then
err.clear
With objStream
.Charset=fileCode:.Type=2:.Mode=3:.Open:.Position=0
.WriteText content:.SaveToFile Server.MapPath(fileDir), 2
.Close
End With
end if
if Err Then  createTextFile=false : errid=err.number:errdes=err.description:Err.Clear : echoErr err_writefile,errid,errdes else createTextFile=true
End Function

'建立文件存储对象函数
Function createStreamFile(Byval stream,Byval fileDir)
dim errid,errdes
fileDir=replace(fileDir, "\", "/")
call createfolder(fileDir,"filedir")
on error resume next
With objStream
.Type =1
.Mode=3 
.Open
.write stream
.SaveToFile server.mappath(fileDir),2
.close
End With
if Err Then  error.clear:createStreamFile=false else createStreamFile=true
End  Function

'建立文件目录函数
Function createFolder(Byval dir,Byval dirType)
dim subPathArray,lenSubPathArray, pathDeep, i
on error resume next
dir=replace(dir, "\", "/")
dir=replace(server.mappath(dir), server.mappath("/"), "")
subPathArray=split(dir, "\")
pathDeep=pathDeep&server.mappath("/")
select case dirType
case "filedir"
lenSubPathArray=ubound(subPathArray) - 1
case "folderdir"
lenSubPathArray=ubound(subPathArray)
end select
for i=1 to  lenSubPathArray
pathDeep=pathDeep&"\"&subPathArray(i)
if not objFso.FolderExists(pathDeep) then objFso.CreateFolder pathDeep
next
if Err Then  createFolder=false : errid=err.number:errdes=err.description:Err.Clear : echoErr err_createFolder,errid,errdes else createFolder=true
End Function

'检查文件是否存在函数
Function isExistFile(Byval fileDir)
on error resume next
If (objFso.FileExists(server.MapPath(fileDir))) Then  isExistFile=True  Else  isExistFile=False
if err then err.clear:isExistFile=False
End Function

'检查文件目录是否存在函数
Function isExistFolder(Byval folderDir)
on error resume next
If objFso.FolderExists(server.MapPath(folderDir)) Then  isExistFolder=True Else isExistFolder=False
if err then err.clear:isExistFolder=False
End Function

'删除文件目录函数
Function delFolder(Byval folderDir)
on error resume next
If isExistFolder(folderDir)=True Then 
objFso.DeleteFolder(server.mappath(folderDir))
if Err Then  delFolder=false : errid=err.number : errdes=err.description:Err.Clear : echoErr err_delFolder,errid,errdes else delFolder=true
else
delFolder=false : die(err_notExistFolder)
end if
End Function

'删除文件函数
Function delFile(Byval fileDir)
on error resume next
If isExistFile(fileDir)=True Then objFso.DeleteFile(server.mappath(fileDir))
if  Err Then  delFile=false : errid=err.number : errdes=err.description:Err.Clear : echoErr err_delFile,errid,errdes else delFile=true
End Function

'检查文件是否存在函数
Function initializeAllObjects()
dim errid,errdes
on error resume next
if not isobject(objFso) then set objFso=server.createobject(FSO_OBJ_NAME)
If Err Then errid=err.number:errdes=err.description:Err.Clear:echoErr err_fsoobj,errid,errdes
if not isobject(objStream) then Set objStream=Server.CreateObject(STREAM_OBJ_NAME)
If Err Then errid=err.number:errdes=err.description:Err.Clear:echoErr err_stmobj,errid,errdes
End Function

'检查文件操作组件函数
Function terminateAllObjects()
on error resume next
if conn.isConnect then conn.close
if isobject(conn) then : set conn=nothing
if isobject(objFso) then set objFso=nothing
if isobject(objStream) then set objStream=nothing
if isobject(cacheObj) then set cacheObj=nothing
if isobject(mainClassObj) then set mainClassObj=nothing
if isObject(gXmlHttpObj) then SET gXmlHttpObj=Nothing
End Function

'移动文件目录函数
Function moveFolder(oldFolder,newFolder)
dim voldFolder,vnewFolder
voldFolder=oldFolder
vnewFolder=newFolder
on error resume next
if voldFolder <> vnewFolder then
voldFolder=server.mappath(oldFolder)
vnewFolder=server.mappath(newFolder)
if not objFso.FolderExists(vnewFolder) then createFolder newFolder,"folderdir"
if  objFso.FolderExists(voldFolder)  then  objFso.CopyFolder voldFolder,vnewFolder : objFso.DeleteFolder(voldFolder)
if Err Then  moveFolder=false : errid=err.number : errdes=err.description:Err.Clear : echoErr err_moveFolder,errid,errdes else moveFolder=true
end if
End Function

'移动文件函数
Function moveFile(ByVal src,ByVal target,Byval operType)
dim srcPath,targetPath
srcPath=Server.MapPath(src)
targetPath=Server.MapPath(target)
if isExistFile(src) then
objFso.Copyfile srcPath,targetPath
if operType="del" then  delFile src
moveFile=true
else
moveFile=false
end if
End Function

'取得文件夹列表函数
Function getFolderList(Byval cDir)
dim filePath,objFolder,objSubFolder,objSubFolders,i
i=0
redim  folderList(0)
filePath=server.mapPath(cDir)
set objFolder=objFso.GetFolder(filePath)
set objSubFolders=objFolder.Subfolders
for each objSubFolder in objSubFolders
ReDim Preserve folderList(i)
With objSubFolder
folderList(i)=.name&",文件夹,"&.size/1000&"KB,"&.DateLastModified&","&cDir&"/"&.name
End With
i=i + 1
next
set objFolder=nothing
set objSubFolders=nothing
getFolderList=folderList
End Function

'取得文件列表函数
Function getFileList(Byval cDir)
dim filePath,objFolder,objFile,objFiles,i
i=0
redim  fileList(0)
filePath=server.mapPath(cDir)
set objFolder=objFso.GetFolder(filePath)
set objFiles=objFolder.Files
for each objFile in objFiles
ReDim Preserve fileList(i)
With objFile
fileList(i)=.name&","&Mid(.name, InStrRev(.name, ".") + 1)&","&.size/1000&"KB,"&.DateLastModified&","&cDir&"/"&.name
End With
i=i + 1
next
set objFiles=nothing
set objFolder=nothing
getFileList=fileList
End Function

'读取文件内容函数
Function loadFile(ByVal filePath)
dim errid,errdes
On Error Resume Next
With objStream
.Type=2
.Mode=3
.Open
.Charset="gbk"
'die Server.MapPath(filePath)
.LoadFromFile Server.MapPath(filePath)
'If Err Then  errid=err.number:errdes=err.description:Err.Clear:echoErr err_loadfile,errid,errdes
.Position=0
loadFile=.ReadText
.Close
End With
End Function
%>

当前名称:ASP常用文件操作函数
文章URL:https://www.cdcxhl.com/news38/285588.html

成都网站建设公司_创新互联,为您提供域名注册自适应网站品牌网站制作企业建站网站内链动态网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站托管运营