ASP FSO操作文件复制文件重命名文件删除文件替换字符串.docx

上传人:b****3 文档编号:12650240 上传时间:2023-04-21 格式:DOCX 页数:11 大小:17.19KB
下载 相关 举报
ASP FSO操作文件复制文件重命名文件删除文件替换字符串.docx_第1页
第1页 / 共11页
ASP FSO操作文件复制文件重命名文件删除文件替换字符串.docx_第2页
第2页 / 共11页
ASP FSO操作文件复制文件重命名文件删除文件替换字符串.docx_第3页
第3页 / 共11页
ASP FSO操作文件复制文件重命名文件删除文件替换字符串.docx_第4页
第4页 / 共11页
ASP FSO操作文件复制文件重命名文件删除文件替换字符串.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

ASP FSO操作文件复制文件重命名文件删除文件替换字符串.docx

《ASP FSO操作文件复制文件重命名文件删除文件替换字符串.docx》由会员分享,可在线阅读,更多相关《ASP FSO操作文件复制文件重命名文件删除文件替换字符串.docx(11页珍藏版)》请在冰豆网上搜索。

ASP FSO操作文件复制文件重命名文件删除文件替换字符串.docx

ASPFSO操作文件复制文件重命名文件删除文件替换字符串

ASPFSO操作文件(复制文件、重命名文件、删除文件、替换字符串)

FSO的意思是FileSystemObject,即文件系统对象。

FSO对象模型包含在Scripting类型库(Scrrun.Dll)中,它同时包含了Drive、Folder、File、FileSystemObject和TextStream五个对象,用来操作文件和文件夹十分方便。

(请注意后面的常用函数)

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对象,以便读取或者追加

重命名文件:

FunctionreName(sourceName,destName)

dimoFso,oFile

setoFso=server.createobject("Scripting.FileSystemObject")

setoFile=oFso.getFile(Server.mappath(sourceName))

oFile.Name=destName

SetoFso=Nothing

SetoFile=Nothing

EndFunction

删除文件:

FunctionFSOdel(fileName)

dimfso,f

setfso=server.CreateObject("scripting.filesystemobject")

f=server.MapPath(fileName)

iffso.FileExists(f)then

fso.DeleteFilef,true

endif

setf=nothing

setfso=nothing

EndFunction

替换文件中的字符串:

FunctionFSOreplace(fileName,Target,repString)

DimobjFSO,objCountFile,FiletempData

SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")

SetobjCountFile=objFSO.OpenTextFile(Server.MapPath(fileName),1,True)

FiletempData=objCountFile.ReadAll

objCountFile.Close

FiletempData=Replace(FiletempData,Target,repString)

SetobjCountFile=objFSO.CreateTextFile(Server.MapPath(fileName),True)

objCountFile.WriteFiletempData

objCountFile.Close

SetobjCountFile=Nothing

SetobjFSO=Nothing

EndFunction

<%

   

   '*******************************************************

   '函数名:

CreateFolder(sPath)

   '作用:

创建目录

   '参数:

sPath:

创建的相对目录路径

   '返回值:

成功true,失败false

   '*******************************************************

   'response.Writecreatefolder("/dgsunshine/UploadFile/demo1/")

   FunctionCreateFolder(sPath)

       OnErrorResumeNext

       

       DimFso,Arrfolder,Folder,i,j   

       IfsPath=""then

           CreateFolder=False

           ExitFunction

       EndIf

       

       IfLeft(sPath,1)="/"Then

           Folder="/"

           sPath=Mid(sPath,2,Len(sPath))

       Else

           Folder="./"

       EndIf

       

       ifRight(sPath,1)="/"thensPath=Left(sPath,Len(sPath)-1)

       ArrFolder=Split(sPath,"/")

       

       SetFso=Server.CreateObject("Scripting.FileSystemObject")

       

       Fori=0ToUbound(ArrFolder)

           Ifi=0then

               Folder=Folder&ArrFolder(i)&"/"

           Else

               Folder=Folder&ArrFolder(i)&"/"

           EndIf

           IfFso.folderExists(Server.MapPath(Folder))=Falsethen

               response.Writeserver.MapPath(folder)

               Fso.createFolder(Server.MapPath(Folder))

           EndIf

           

       Next

       SetFso=nothing

       

       IfErr.Number<>0then

           Err.clear()

           CreateFolder=False

       Else

           CreateFolder=True

       EndIf

   Endfunction

   FunctiongetFile(paramFilePath)

       SetFso=Server.CreateObject("Scripting.FileSystemObject")

       SetFso_Read=fso.OpenTextFile(Server.MapPath(paramFilePath),1,false,-2)

       getFile=Fso_Read.readall

       SetFso_Read=Nothing

       SetFso=Nothing

   EndFunction

   

   '*******************************************************

   '函数名:

CreateFile(paramFileContent,paramFilePath)

   '作用:

创建文件

   '参数:

paramFileContent'文件的内容

   '       paramFilePath    '文件名(不包括路径)

   '返回值:

成功true,失败false

   '*******************************************************

    FunctionCreateFile(paramFileContent,paramFilePath)

       OnErrorResumeNext

       DimFso,fWrite

       SetFso=Server.CreateObject("Scripting.FileSystemObject")

       SetfWrite=Fso.CreateTextFile(Server.Mappath(paramFilePath),true)

       fWrite.writeparamFileContent

       fWrite.close()

       SetfWrite=nothing

       SetFso=nothing

       IfErr.number<>0Then

           Err.clear()

           CreateFile=False

       Else

           CreateFile=True

       EndIf  

   EndFunction

   

   '*******************************************************

   '函数名:

DelFile(FilePath)

   '作用:

删除文件

   '参数:

FilePath'文件路径多个文件用"|"隔开

   '返回值:

成功true,失败false

   '*******************************************************

   FunctionDelFile(FilePath)

       OnErrorResumeNext

       Dimfso,arrFile,i

       

       IfGetSafeStr(FilePath,"")=""then

           CreateFolder=false

           ExitFunction

       EndIf

       

       arrFile=Split(FilePath,"|")

       SetFso=Server.CreateObject("Scripting.FileSystemObject")

       

       fori=0toUBound(arrFile)

           FilePath=arrFile(i)

           IfFso.FileExists(Server.MapPath(FilePath))then

               Fso.DeleteFileServer.MapPath(FilePath)

           EndIf

       Next

       Setfso=nothing

       

       IfErrthen

           Err.clear()

           DelFile=false

       Else

           DelFile=true

       EndIf

   EndFunction

   

   '*******************************************************

   '函数名:

DelFolder(FolderPath)

   '作用:

删除目录

   '参数:

FolderPath'目录路径'多个目录用"|"分隔

   '返回值:

成功true,失败false

   '*******************************************************

   FunctionDelFolder(FolderPath)

       OnErrorResumeNext

       DimFso,arrFolder,i

       

       IfGetSafeStr(FolderPath,"")=""then

           DelFolder=false

           ExitFunction

       EndIf

       

       arrFolder=Split(FolderPath,"|")

       SetFso=Server.CreateObject("Scripting.FileSystemObject")

       

       Fori=0toUBound(arrFolder)

           FolderPath=arrFolder(i)

           IfFso.folderexists(Server.MapPath(FolderPath))then

               Fso.deleteFolderServer.MapPath(FolderPath)

           EndIf

       Next

       

       IfErrthen

           Err.clear()

           DelFolder=false

           'ShowError"删除目录失败",""

       else

           DelFolder=true

       EndIf

   EndFunction

   '*******************************************************

   '函数名:

IsExistFile(FilePath)

   '作用:

判断文件或目录是否存在

   '参数:

FilePath'文件路径多个文件用"|"隔开

   '返回值:

成功true,失败false

   '*******************************************************

   FunctionIsExistFile(FilePath)

       OnErrorResumeNext

       Dimfso,arrFile,i

       IfGetSafeStr(FilePath,"")=""then

           IsExistFile=false

       EndIf

       arrFile=Split(FilePath,"|")

       SetFso=Server.CreateObject("Scripting.FileSystemObject")

       

       fori=0toUBound(arrFile)

           FilePath=arrFile(i)

           IfFso.FileExists(Server.MapPath(FilePath))then

               IsExistFile=True

           EndIf

           IfFso.folderexists(Server.MapPath(FilePath))then

               IsExistFile=True

           EndIf

       Next

       Setfso=nothing

       

       IfErrthen

           Err.clear()

           IsExistFile=false

           'ShowError"判断文件或目录是否存在失败",""

       else

           IsExistFile=true

       EndIf

   EndFunction

   

   

   '*******************************************************

   '函数名:

DelFile(FilePath)

   '作用:

删除文件或目录

   '参数:

FilePath'文件路径多个文件用"|"隔开

   '返回值:

成功true,失败false

   '*******************************************************

   FunctionDelFile(FilePath)

       OnErrorResumeNext

       Dimfso,arrFile,i

       

       IfGetSafeStr(FilePath,"")=""then

           CreateFolder=false

       EndIf

       

       arrFile=Split(FilePath,"|")

       SetFso=Server.CreateObject("Scripting.FileSystemObject")

       

       fori=0toUBound(arrFile)

           FilePath=arrFile(i)

           IfFso.FileExists(Server.MapPath(FilePath))then

               Fso.DeleteFileServer.MapPath(FilePath)

           EndIf

           IfFso.folderexists(Server.MapPath(FilePath))then

               Fso.deleteFolderServer.MapPath(FilePath)

           EndIf

       Next

       Setfso=nothing

       

       IfErrthen

           Err.clear()

           DelFile=false

           'ShowError"删除文件或目录失败",""

       else

           DelFile=true

       EndIf

   EndFunction

   

   

   '*******************************************************

   '函数名:

ReNameFile((oldName,newName)

   '作用:

重命名文件或目录

   '参数:

strOldName'原文件名多个用"|"隔开

   '       strNewName'新文件名多个用"|"隔开

   '       上面两个参数请保持一致

   '返回值:

成功true,失败false

   '*******************************************************

   FunctionReNameFile(strOldName,strNewName)

       OnErrorResumeNext

       Dimfso,arrOld,arrNew,i,oldName,newName

       

       old=GetSafeStr(strOldName,"")

       Newfile=GetSafeStr(strNewName,"")

       

       Ifold=""orNewfile=""then

           ReNameFile=false

           ExitFunction

       EndIf

       

       arrOld=Split(strOldName,"|")

       arrNew=Split(strNewName,"|")

       

       IfUBound(arrOld)<>UBound(arrNew)then

           ReNameFile=false

           ExitFunction

       EndIf

       

       SetFso=Server.CreateObject("Scripting.FileSystemObject")

       fori=0toUBound(arrOld)

           oldName=Server.MapPath(arrOld(i))

           newName=Server.MapPath(arrNew(i))

           IfFso.FileExists(oldName)andnotFso.FileExists(newName

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 中医中药

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1