直接上代碼
<% '建立文件夾函數(shù) Function CreateFolder(strFolder)'參數(shù)為相對路徑 '首選判斷要建立的文件夾是否已經(jīng)存在 Dim strTestFolder,objFSO strTestFolder = Server.Mappath(strFolder) Set objFSO = CreateObject("Scripting.FileSystemObject") '檢查文件夾是否存在 If not objFSO.FolderExists(strTestFolder) Then '如果不存在則建立文件夾 objFSO.CreateFolder(strTestFolder) End If Set objFSO = Nothing End function '刪除文件夾 Function DelFolder(strFolder)'參數(shù)為相對路徑 strTestFolder = Server.Mappath(strFolder) Set objFSO = CreateObject("Scripting.FileSystemObject") '檢查文件夾是否存在 If objFSO.FolderExists(strTestFolder) Then objFSO.DeleteFolder(strTestFolder) end if Set objFSO = Nothing End function '創(chuàng)建文本文件 Function Createtextfile(fileurl,filecontent)'參數(shù)為相對路徑和要寫入文件的內(nèi)容 Set objFSO = Server.CreateObject("Scripting.FileSystemObject") Set fout = objFSO.CreateTextFile(Server.MapPath(fileurl)) fout.WriteLine filecontent fout.close Set objFSO = Nothing End Function '刪除文件(適合所有文件) Function Deltextfile(fileurl)'參數(shù)為相對路徑 Set objFSO = CreateObject("Scripting.FileSystemObject") fileurl = Server.MapPath(fileurl) if objFSO.FileExists(fileurl) then '檢查文件是否存在 objFSO.DeleteFile(Server.mappath(fileurl)) end if Set objFSO = nothing End Function '建立圖片文件并保存圖片數(shù)據(jù)流 Function Createimage(fileurl,imagecontent)'參數(shù)為相對路徑和文件內(nèi)容 Set objStream = Server.CreateObject("ADODB.Stream") '建立ADODB.Stream對象,必須要ADO 2.5以上版本 objStream.Type =1 '以二進(jìn)制模式打開 objStream.Open objstream.write imagecontent '將字符串內(nèi)容寫入緩沖 objstream.SaveToFile server.mappath(fileurl),2 '-將緩沖的內(nèi)容寫入文件 objstream.Close()'關(guān)閉對象 set objstream=nothing End Function '遠(yuǎn)程獲取文件數(shù)據(jù) Function getHTTPPage(url) 'On Error Resume Next dim http set http=Server.createobject("Microsoft.XMLHTTP") Http.open "GET",url,false Http.send() if Http.readystate<>4 then exit function end if getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") set http=nothing If Err.number<>0 then getHTTPPage = "服務(wù)器獲取文件內(nèi)容出錯" Err.Clear End If End function Function BytesToBstr(body,Cset) dim objstream set objstream = Server.CreateObject("adodb.stream") objstream.Type = 1 objstream.Mode =3 objstream.Open objstream.Write body objstream.Position = 0 objstream.Type = 2 objstream.Charset = Cset BytesToBstr = objstream.ReadText objstream.Close set objstream = nothing End Function '獲取圖片數(shù)據(jù)流 Function getpic(url) on error resume next dim http set http=server.createobject("MSXML2.XMLHTTP")'使用xmlhttp的方法來獲得圖片的內(nèi)容 Http.open "GET",url,false Http.send() if Http.readystate<>4 then exit function end if getpic=Http.responseBody set http=nothing if err.number<>0 then getpic = "服務(wù)器獲取文件內(nèi)容出錯" err.Clear End if End Function '打開文件(文本形式) Function OpenFile(fileurl)'文件相對路徑 Dim Filename,fso,hndFile Filename = fileurl Filename = Server.MapPath(Filename) Set objfso = CreateObject("Scripting.FileSystemObject") If objfso.FileExists(Filename) Then set hndFile = objfso.OpenTextFile(Filename) OpenFile = hndFile.ReadAll Else OpenFile = "文件讀取錯誤" End If Set hndFile = Nothing Set objfso = Nothing End Function '獲得文件的后綴名 function getFileExtName(fileName) dim pos pos=instrrev(filename,".") if pos>0 then getFileExtName=mid(fileName,pos+1) else getFileExtName="" end if end function Dim fso,f,folder Set fso=Server.CreateObject("scripting.filesystemobject") '改目錄名 Set folder=fso.getfolder(Server.Mappath("Old")) folder.name="New" '新名字 '改文件名 Set f=fso.getfile(Server.Mappath("Old.asp")) f.name="New.asp" '新名字 '釋放 function copyfile(l1,l2)'復(fù)制文件 on error resume next dim fs set fs=createobject("Scripting.FileSystemObject") fs.copyfile server.mappath(l1),server.mappath(l2) set fs=nothing if err.number<>0 then err.clear end functioncopyfile("db1.mdb","db2.mdb") 具體的大家可以搜索
FSO操作全集
下面的代碼也適合vbs
<script language=vbs>on error resume nextSet fso=CreateObject("Scripting.FileSystemObject") '使FSO組件可以被fso 變量調(diào)用getfso=fso.DriveExists("g:/") '判斷指定硬盤驅(qū)動器是否存在getfso=fso.GetDrive("c:") '創(chuàng)建自定義的FSO驅(qū)動器對象getfso=fso.GetDriveName("c:/網(wǎng)絡(luò)程序員伴侶") '返回文件夾的所在盤符Set getfso=fso.Drives '創(chuàng)建FSO驅(qū)動器集合對象,多配合for each i in getfso語句進(jìn)行窮盡操作,支持所有[驅(qū)動器對象屬性],并具有Count和Item屬性 Set fso=Nothing '釋放fso變量與FSO組件的連接資源</script>驅(qū)動器對象操作
<script language=vbs>on error resume nextSet fso=CreateObject("Scripting.FileSystemObject") '使FSO組件可以被fso變量調(diào)用getfso=fso.GetDrive("c:").FileSystem '返回驅(qū)動器對象的文件系統(tǒng)類型getfso=fso.GetDrive("c:").DriveType '返回驅(qū)動器對象的驅(qū)動器類型,返回值范圍0-5getfso=fso.GetDrive("c:").DriveLetter '返回驅(qū)動器對象的字母getfso=fso.GetDrive("c:").IsReady '返回指定驅(qū)動器是否準(zhǔn)備好或是否損壞getfso=fso.GetDrive("c:").SerialNumber '返回驅(qū)動器對象的唯一十進(jìn)制卷標(biāo)序號getfso=fso.GetDrive("c:").ShareName '返回驅(qū)動器對象的網(wǎng)絡(luò)共享名getfso=fso.GetDrive("c:").VolumeName '返回驅(qū)動器對象的卷標(biāo)名,同時也可以設(shè)置其卷標(biāo)名getfso=fso.GetDrive("c:").Path '返回驅(qū)動器的實(shí)際路徑,如c:/則返回c:getfso=fso.GetDrive("c:").RootFolder '返回驅(qū)動器對象的跟文件夾getfso=fso.GetDrive("c:").AvailableSpace'返回驅(qū)動器對象的可用容量大小getfso=fso.GetDrive("c:").FreeSpace '返回驅(qū)動器對象的剩余空間大小getfso=fso.GetDrive("c:").TotalSize '返回驅(qū)動器對象的總空間容量大小 Set fso=Nothing '釋放fso變量與FSO組件的連接資源</script>系統(tǒng)文件夾操作
<script language=vbs>on error resume nextSet fso=CreateObject("Scripting.FileSystemObject") '使FSO組件可以被fso變量調(diào)用getfso=fso.GetFolder("c:/") '創(chuàng)建自定義的FSO文件夾對象getfso=fso.FolderExists("e:/網(wǎng)絡(luò)程序員伴侶") '判斷指定文件夾是否存在getfso=fso.GettempName '隨機(jī)返回WINDOW產(chǎn)生在temp文件夾中的臨時文件getfso=fso.GetparentFolderName("e:/網(wǎng)絡(luò)程序員伴侶") '返回指定文件夾的父文件夾fso.CreateFolder "c:/windows/新創(chuàng)建的文件夾路徑及名稱"fso.MoveFolder "c:/windows/許移動或改名的文件夾","c:/windows/移動路徑和新的文件夾名"fso.DeleteFolder "c:/windows/要刪除的目錄名",false/truefso.CopyFolder "c:/windows/須復(fù)制的目錄名","c:/windows/得到付值內(nèi)容的目錄名",false/trueSet getfso=fso.Folders '創(chuàng)建FSO文件夾集合對象,多配合for each i in getfso語句進(jìn)行窮盡操作,支持所有[文件夾對象屬性],并具有Count和Item屬性,具有AddFolders方法Set getfso=fso.Files '創(chuàng)建FSO文件夾內(nèi)文件集合對象,多配合for each i in getfso語句進(jìn)行窮盡操作,支持所有[文件對象屬性],并具有Count和Item屬性Set fso=Nothing '釋放fso變量與FSO組件的連接資源</script>文件夾對象操作
<script language=vbs>on error resume nextSet fso=CreateObject("Scripting.FileSystemObject") '使FSO組件可以被fso變量調(diào)用getfso=fso.GetFolder("c:/windows").Name '返回文件夾的名字,這里返回WINDOWgetfso=fso.GetFolder("c:/windows").ShortName '返回文件夾[8.3]格式的名稱getfso=fso.GetFolder("c:/windows").Attributes '返回文件夾的屬性,可返回0,1,2,4,8,16,32,64,128getfso=fso.GetFolder("c:/windows").Size '返回文件夾(包括子文件夾)內(nèi)文件占用空間大小getfso=fso.GetFolder("c:/windows").Type '返回文件夾類型信息getfso=fso.GetFolder("c:/windows").Path '返回文件夾的真實(shí)路徑getfso=fso.GetFolder("c:/windows").ShortPath '返回文件夾[8.3]格式的路徑getfso=fso.GetFolder("c:/windows").Drive '返回文件夾所在驅(qū)動器getfso=fso.GetFolder("c:/windows").Files.count '返回文件夾包含的所有文件對象集合getfso=fso.GetFolder("c:/windows").SubFolders.count '返回文件夾包含的所有子文件夾的對象集合getfso=fso.GetFolder("c:/windows").ParentFolder '返回文件夾的父文件夾對象getfso=fso.GetFolder("c:/windows").IsRootFolder '返回文件夾是否為跟文件夾,是返回true否返回falsegetfso=fso.GetFolder("c:/windows").DateCreated '返回文件夾的最初創(chuàng)建時間getfso=fso.GetFolder("c:/windows").DateLastAccessed '返回文件夾最后一次訪問時間getfso=fso.GetFolder("c:/windows").DateLastModified '返回文件夾最近修改的時間fso.GetFolder("c:/windows").CreateTextFile "新建的文件名及后綴",true/false,true/falsefso.GetFolder("c:/windows/要刪除的目錄名").Delete truefso.GetFolder("c:/windows/要移動改名的目錄名").Move "文件夾將要移動到的路徑及自定義文件夾名稱"fso.GetFolder("c:/windows/舊目錄").Copy "復(fù)制到路徑",true/false '在指定路徑付值此文件夾,true=覆蓋,false相反Set fso=Nothing '釋放fso變量與FSO組件的連接資源</script>系統(tǒng)文件操作
<script language=vbs>on error resume nextSet fso=CreateObject("Scripting.FileSystemObject") '使FSO組件可以被fso變量調(diào)用getfso=fso.GetFile("c:/windows/help.html") '得到文件完整路徑getfso=fso.GetFileName("c:/windows/help.html") '得到文件名稱和后綴getfso=fso.GetExtensionName("c:/windows/help.html") '得到文件的文件類型(后綴),不含小數(shù)點(diǎn)getfso=fso.FileExists("c:/windows/文件名.html") '判斷文件是否存在fso.MoveFile "要移動及改名的原文件路徑","移動到某路徑及自定義新文件名"fso.DeleteFile "c:/windows/要刪除的文件所在路徑",false/truefso.CopyFile "c:/windows/須復(fù)制的舊文件.txt","c:/windows/復(fù)制后的文件名.jpg",false/truefso.OpenTextFile "c:/windows/desktop/要打開操作的文件名稱.txt",1/2/8,true/false,0/-1/-2 'ForReading=1=只讀方式,ForWriting=2=可寫方式,ForAppending=8=追加方式.true=如果打開的文件不存在則創(chuàng)建該文件.TristateFalse=0=以系統(tǒng)默認(rèn)方式打開,TristateTrue=-1=以Unicode格式打開文件,TristateUseDefaule=-2=以ASCLL格式打開文件(缺剩值)fso.CreateTextFile "c:/windows/desktop/新建的文件名稱.txt",false/true,false/true '前者true覆蓋以存在同名文件,后者為true新文件將以Unicode方式創(chuàng)建,反之=結(jié)果相反Set fso=Nothing '釋放fso變量與FSO組件的連接資源</script>文件對象操作
<script language=vbs>on error resume nextSet fso=CreateObject("Scripting.FileSystemObject") '使FSO組件可以被fso變量調(diào)用getfso=fso.GetFile("c:/windows/筆記本.txt").Name '返回文件對象的名稱包含后綴getfso=fso.GetFile("c:/windows/筆記本.txt").ShortName '返回文件對象的[8.3]名稱包含后綴getfso=fso.GetFile("c:/windows/筆記本.txt").Attributes '返回文件對象的屬性,可返回0,1,2,4,8,16,32,64,128getfso=fso.GetFile("c:/windows/筆記本.txt").Size '返回文件對象的大小getfso=fso.GetFile("c:/windows/筆記本.txt").Type '返回文件對象的類型getfso=fso.GetFile("c:/windows/筆記本.txt").Path '返回文件對象的完整路徑getfso=fso.GetFile("c:/windows/筆記本.txt").ShortPath '返回文件對象的[8.3]的完整路徑getfso=fso.GetFile("c:/windows/筆記本.txt").Drive '返回此文件對象所在的驅(qū)動器getfso=fso.GetFile("c:/windows/筆記本.txt").ParentFolder '返回文件對象所在的文件夾getfso=fso.GetFile("c:/windows/筆記本.txt").DateCreated '返回文件對象的創(chuàng)建日期getfso=fso.GetFile("c:/windows/筆記本.txt").DateLastAccessed '返回文件對象的最后訪問日期getfso=fso.GetFile("c:/windows/筆記本.txt").DateLastModified '返回文件對象的最后修改時間fso.GetFile("c:/windows/筆記本.txt").Copy "復(fù)制到的路徑和文件名",true/falsefso.GetFile("c:/windows/筆記本.txt").Delete truefso.GetFile("c:/windows/筆記本.txt").Move "移動到的路徑及自定義文件名Set fso=Nothing '釋放fso變量與FSO組件的連接資源</script> 文件對象讀寫操作
<script language=vbs>on error resume nextSet fso=CreateObject("Scripting.FileSystemObject") '使FSO組件可以被fso變量調(diào)用set link1=fso.GetFile("c:/windows/筆記本.txt").OpenAsTextStream(1/2/8,0/-1/-2)open1=link1.AtendOfline '判斷文件指針是否以在行的末尾open2=link1.AtendOfstream '判斷文件指針是否一再文件的末尾open3=link1.Column '返回當(dāng)前字符所在文件的列號open4=link1.Line '返回當(dāng)前字符所在文件的行號open5=link1.Read(10) '讀取文件中指定數(shù)量的字符open6=link1.ReadAll '讀取文件中所有的字符open7=link1.ReadLine '讀取文件中一行中含有的字符open8=link1.Write "字符串" '將自定字符串或字符串變量寫入文件open9=link1.WriteLine "字符" '將自定字符+一個換行符寫入文件open10=link1.WriteBlankLines 10 '將指定數(shù)量的換行符寫入文件open11=link1.Skip 10 '使文件指針跳過指定數(shù)量的字符open12=link1.SkipLine '使文件指針跳到下一行l(wèi)ink1.Close '文件操作完畢,銷毀文件對象指針Set fso=Nothing '釋放fso變量與FSO組件的連接資源</script>以上就是ASP FSO文件處理函數(shù)大全的詳細(xì)內(nèi)容,更多關(guān)于ASP FSO文件處理的資料請關(guān)注武林網(wǎng)其它相關(guān)文章!
新聞熱點(diǎn)
疑難解答
圖片精選