ASP FSO檔案處理函數大全

來源:互聯網
上載者:User

複製代碼 代碼如下:<%
'建立檔案夾函數
Function CreateFolder(strFolder)'參數為相對路徑
'首選判斷要建立的檔案夾是否已經存在
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)'參數為相對路徑
strTestFolder = Server.Mappath(strFolder)
Set objFSO = CreateObject("Scripting.FileSystemObject")
'檢查檔案夾是否存在
If objFSO.FolderExists(strTestFolder) Then
objFSO.DeleteFolder(strTestFolder)
end if
Set objFSO = Nothing
End function

'建立文字檔
Function Createtextfile(fileurl,filecontent)'參數為相對路徑和要寫入檔案的內容
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)'參數為相對路徑
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

'建立圖片檔案並儲存圖片資料流
Function Createimage(fileurl,imagecontent)'參數為相對路徑和檔案內容
Set objStream = Server.CreateObject("ADODB.Stream") '建立ADODB.Stream對象,必須要ADO 2.5以上版本
objStream.Type =1 '以二進位模式開啟
objStream.Open
objstream.write imagecontent '將字串內容寫入緩衝
objstream.SaveToFile server.mappath(fileurl),2 '-將緩衝的內容寫入檔案
objstream.Close()'關閉對象
set objstream=nothing
End Function

'遠程擷取檔案資料
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 = "伺服器擷取檔案內容出錯"
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

'擷取圖片資料流
Function getpic(url)
on error resume next
dim http
set http=server.createobject("MSXML2.XMLHTTP")'使用xmlhttp的方法來獲得圖片的內容
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 = "伺服器擷取檔案內容出錯"
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
%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.