首页 电脑 电脑学堂 查看内容

asp之FSO大全

2008-11-22 11:20 654 0

摘要: 对于一个支持asp和fso的空间来说有了fso一切变得简单多了我也是个新手写了一些代码供大家学习研究用首先看支持fso组件吗<%'FSO组件名称dim FSObjectFSObject="Scr...
关键词: fso Function 文件 CreateObject dbPath 路径 newfile strDBPath Server MapPath

对于一个支持asp和fso的空间来说有了fso一切变得简单多了我也是个新手写了一些代码供大家学习研究用首先看支持fso组件吗<%'FSO组件名称dim FSObjectFSObject="Scripting.FileSystemObject"'========================================================='◆是否支持组件'=========================================================Function IsObjInstalled(strClassString)On Error Resume NextIsObjInstalled = FalseErr = 0Dim xTestObjSet xTestObj = Server.CreateObject(strClassString)If 0 = Err Then IsObjInstalled = TrueSet xTestObj = NothingErr = 0End Functionif IsObjInstalled(FSObject) thenresponse.write "√"elseresponse.write "×"end if%>-------------------------------------------------------<%'========================================================='◆是否支持组件'=========================================================Function IsObjInstalled(strClassString)On Error Resume NextIsObjInstalled = FalseErr = 0Dim xTestObjSet xTestObj = Server.CreateObject(strClassString)If 0 = Err Then IsObjInstalled = TrueSet xTestObj = NothingErr = 0End Function'========================================================='fso 操作'========================================================='◆检查某一目录是否存在'=========================================================Function CheckDir(FolderPath)folderpath=Server.MapPath(".")&"\"&folderpathSet fso= CreateObject(FSObject)If fso.FolderExists(FolderPath) thenCheckDir = TrueElseCheckDir = FalseEnd ifSet fso= nothingEnd Function'========================================================='◆ 根据指定名称生成目录'=========================================================Function MakeNewsDir(foldername)dim fs0Set fso= CreateObject(FSObject)Set fs0= fso.CreateFolder(foldername)Set fso = nothingEnd Function'========================================================='◆ 如果文件夹不存在则建立新文件夹 ◆'=========================================================Function checkFolder(folderpath)If CheckDir(folderpath) = false Then'如果文件夹不存在MakeNewsDir(folderpath)'就建一个文件夹end ifend Function'========================================================='◆ 删除文件夹 ◆'=========================================================Function DeleteFoldera(folderpath)dim pathSet fso = CreateObject(FSObject)path=request.ServerVariables("APPL_PHYSICAL_PATH")&folderpathfso.DeleteFolder(path)Set fso = nothingend Function'========================================================='◆ 更改文件夹名称 ◆'=========================================================Function moveFolder(foldername,newfoldername)isfsoSet fso = CreateObject(FSObject)fso.moveFolder ""&request.ServerVariables("APPL_PHYSICAL_PATH")&"\"&foldername&"" ,""&request.ServerVariables("APPL_PHYSICAL_PATH")&"\"&newfoldername&""Set fso =nothingEnd Function'========================================================='◆ 删除指定文件 ◆'=========================================================Function DeleteFile(file)Set fso = CreateObject(FSObject)fso.DeleteFile request.ServerVariables("APPL_PHYSICAL_PATH")&fileSet fso = nothingEnd Function'========================================================='◆ 备份指定文件 ◆'=========================================================Function CopyFile(oldfile,newfile)Set fso = CreateObject(FSObject)On Error Resume Next Set fso=Server.CreateObject(FSObject)oldfile=Server.MapPath(oldfile)if Err.Number>0 Then call alert("原路径错误!","")newfile=Server.MapPath(newfile)if Err.Number>0 Then call alert("新路径错误!","")fso.CopyFile oldfile,newfile'覆盖原来的文件if Err.Number>0 Then call alert(Err.Description,"")Set fso=nothingEnd Function'========================================================='◆ 转移指定文件 ◆'=========================================================Function MoveFile(oldfile,newfile)Set fso = CreateObject(FSObject)On Error Resume Next Set fso=Server.CreateObject(FSObject)oldfile=Server.MapPath(oldfile)if Err.Number>0 Then call alert("原路径错误!","")newfile=Server.MapPath(newfile)if Err.Number>0 Then call alert("新路径错误!","")'fso.MoveFile oldfile,newfile'不能覆盖原来的文件fso.MoveFile "d:\o\data\test.txt","d:\o\databackup\test3.txt"if Err.Number>0 Then call alert(Err.Description,"")Set fso=nothingEnd Function'========================================================='◆ 读取文件代码 ◆'=========================================================Function loadfile(file)'读取文件dim ftempSet fso = CreateObject(FSObject)Set ftemp=fso.OpenTextFile(Server.MapPath(""&file&""), 1)loadfile=ftemp.ReadAllftemp.Closefso.closeset fso=nothingEnd Function'========================================================='◆ 根据代码生成文件 ◆'========================================================='========================================'■file生成文件名'■code文件的代码'========================================Function savefile(file,code)'保存文件dim MyFileSet fso = CreateObject(FSObject)Set MyFile = fso.CreateTextFile(Server.mapPath(file), True)MyFile.WriteLine(code)MyFile.Closeset MyFile=nothingEnd Function'========================================================='◆ 压缩数据库 ◆'========================================================='========================================'■dbPath数据文件路径'■boolIs97 access97压缩'========================================Function CompactDB(dbPath,boolIs97)dim strDBPath,fso,EnginedbPath=server.mappath(dbpath)strDBPath = left(dbPath,instrrev(DBPath,"\"))Set fso = CreateObject(FSObject)If fso.FileExists(dbPath) ThenSet Engine = CreateObject("JRO.JetEngine")If boolIs97 = "True" Thendim JET_3XEngine.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath, _"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & "temp.mdb;" _&"Jet OLEDB:Engine Type=" & JET_3XElseEngine.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath, _"Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database password="&dbpw&";Data Source="&strDBPath&"temp.mdb"End Iffso.CopyFile strDBPath & "temp.mdb",dbpathfso.DeleteFile(strDBPath&"temp.mdb")Set fso = nothingSet Engine = nothingCompactDB="当前数据库,已经压缩成功!"ElseCompactDB="数据库名称或路径不正确. 请重试!"End IfEnd Function%>
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部