Asp 對檔案操作問題總結

來源:互聯網
上載者:User
<html><body>讀取一個檔案相關資訊的範例<br><br><%  Dim objFSO,objFile    '聲明一個名稱為 objFSO 的變數以存放對象執行個體  Set objFSO = Server.CreateObject("Scripting.FileSystemObject")   If objFSO.FileExists(Server.MapPath("newfile.asp")) Then     Set objFile = objFSO.GetFile(Server.MapPath("newfile.asp"))     Response.Write "檔案的路徑:"&objFile.Path&"<br>"     Response.Write "檔案的建立日期:"&objFile.DateCreated&"<br>"     Response.Write "檔案的大小:"&objFile.Size&"<br>"     Response.Write "檔案的類型:"&objFile.Type&"<br>"   Else     Response.Write Server.MapPath("newfile.asp")&"不存在,無法讀取相關資訊"   End If  Set objFile = Nothing  Set objFSO = Nothing      '釋放 FileSystemObject 對象執行個體記憶體空間%></body></html>


動網論壇的代碼,你參考一下吧<!--#include file =conn.asp--><!-- #include file="inc/const.asp" --><title><%=Forum_info(0)%>--管理頁面</title><!--#include file=inc/forum_css.asp--><meta NAME=GENERATOR Content="Microsoft FrontPage 3.0" CHARSET=GB2312><BODY <%=Forum_body(11)%>><%if not master or session("flag")="" thenErrmsg=Errmsg+"<br>"+"<li>本頁面為管理員專用,請<a href=admin_index.asp target=_top>登陸</a>後進入。<br><li>您沒有管理本頁面的許可權。"call dvbbs_error()response.endend ifon error resume next Sub ShowSpaceInfo(drvpath) dim fso,d,size,showsize set fso=server.createobject("scripting.filesystemobject")  drvpath=server.mappath(drvpath)   set d=fso.getfolder(drvpath)  size=d.size showsize=size & " Byte"  if size>1024 then    size=(size/1024)    showsize=size & " KB" end if if size>1024 then    size=(size/1024)    showsize=formatnumber(size,2) & " MB" end if if size>1024 then    size=(size/1024)    showsize=formatnumber(size,2) & " GB"    end if    response.write "<font face=verdana>" & showsize & "</font>" End Sub  Sub Showspecialspaceinfo(method) dim fso,d,fc,f1,size,showsize,drvpath  set fso=server.createobject("scripting.filesystemobject") drvpath=server.mappath("pic") drvpath=left(drvpath,(instrrev(drvpath,"/")-1)) set d=fso.getfolder(drvpath)   if method="All" then  size=d.size elseif method="Program" then set fc=d.Files for each f1 in fc size=size+f1.size next end if  showsize=size & " Byte"  if size>1024 then    size=(size/1024)    showsize=size & " KB" end if if size>1024 then    size=(size/1024)    showsize=formatnumber(size,2) & " MB" end if if size>1024 then    size=(size/1024)    showsize=formatnumber(size,2) & " GB"    end if    response.write "<font face=verdana>" & showsize & "</font>" end sub     Function Drawbar(drvpath) dim fso,drvpathroot,d,size,totalsize,barsize set fso=server.createobject("scripting.filesystemobject") drvpathroot=server.mappath("pic") drvpathroot=left(drvpathroot,(instrrev(drvpathroot,"/")-1)) set d=fso.getfolder(drvpathroot) totalsize=d.size  drvpath=server.mappath(drvpath)  set d=fso.getfolder(drvpath) size=d.size  barsize=cint((size/totalsize)*400) Drawbar=barsize End Function   Function Drawspecialbar() dim fso,drvpathroot,d,fc,f1,size,totalsize,barsize set fso=server.createobject("scripting.filesystemobject") drvpathroot=server.mappath("pic") drvpathroot=left(drvpathroot,(instrrev(drvpathroot,"/")-1)) set d=fso.getfolder(drvpathroot) totalsize=d.size  set fc=d.files for each f1 in fc size=size+f1.size next  barsize=cint((size/totalsize)*400) Drawspecialbar=barsize End Function  %><table align=center cellspacing=1 cellpadding=1 class=tableborder1>      <tr>     <th height=25>  系統空間佔用情況</th>  </th>  </tr>   <tr>     <td class=tablebody1>  <blockquote>  <% fsoflag=1 if fsoflag=1 then %> <br>  法規資料佔用空間: <img src="pic/bar1.gif" width=<%=drawbar("data")%> height=10> <%showSpaceinfo("data")%><br><br> 備份資料佔用空間: <img src="pic/bar1.gif" width=<%=drawbar("databackup")%> height=10> <%showSpaceinfo("databackup")%><br><br> 程式檔案佔用空間: <img src="pic/bar1.gif" width=<%=drawspecialbar%> height=10> <%showSpecialSpaceinfo("Program")%><br><br> 心情圖片佔用空間: <img src="pic/bar1.gif" width=<%=drawbar("images")%> height=10> <%showSpaceinfo("face")%><br><br> 系統圖片佔用空間: <img src="pic/bar1.gif" width=<%=drawbar("pic")%> height=10> <%showSpaceinfo("pic")%><br><br> 上傳頭像佔用空間: <img src="pic/bar1.gif" width=<%=drawbar("uploadFace")%> height=10> <%showSpaceinfo("uploadFace")%><br><br> 上傳圖片佔用空間: <img src="pic/bar1.gif" width=<%=drawbar("uploadImages")%> height=10> <%showSpaceinfo("uploadImages")%><br><br> 系統佔用空間總計:<br><img src="pic/bar1.gif" width=400 height=10> <%showspecialspaceinfo("All")%> <% else response.write "<br><li>本功能已經被關閉" end if %> </blockquote>       </td>    </tr></table>


ASP--檔案操作類 設計版


<%
'====================================================
'Version: AspFile 1.0
'Data: 2006.6.20
'CopyRight: (C) 2006 Evan.cn All Rights Reserved
'====================================================
'檔案操作類
'檔案名稱
'檔案大小
'檔案建立
'FileName--檔案名稱
'DestinationFile--目標檔案名稱
'FileSize--獲得檔案大小
'FileCreatedDate--獲得檔案建立日期
'FileAttributes--獲得檔案屬性
'ShowFileAccessInfo(InfoType)--獲得檔案屬性
'CreateTxtFile()--建立文字檔
'WriteTxtFile(TextStr,WriteORAppendType)==寫文字檔
'ReadTxtFile()--讀取文字檔
'DeleteFile()--刪除檔案
'CopyFile()--拷貝檔案
'MoveFile()--移動檔案
'
'
Class Cls_FSO
Private o_Fso,s_FileName,s_FolderName,s_DestinationFile
Private s_FolderName,s_DestinationFolder

Private Sub Class_Initialize()
Set o_Fso = CreateObject("Scripting.FileSystemObject")
End Sub

Private Sub Class_Terminate()
Set o_Fso = Nothing
End Sub

'===============================================檔案操作

'設定檔案名稱字
Public Property Let FileName(Byval Values)
s_FileName=Values
'checkFile '檢查檔案
End Property

'設定目標檔案名
Public Property Let DestinationFile(Byval Values)
s_DestinationFile=Values
End Property


'取檔案大小
Public Property Get FileSize()
If fileExist Then
Dim f
Set f = o_Fso.Getfile(s_FileName)
FileSize = f.Size
Else
FileSize=0
End if
End Property

'取檔案
Public Property Get FileCreatedDate()
If fileExist Then
Dim f
Set f = o_Fso.GetFile(s_FileName)
FileCreatedDate = f.DateCreated
Else
FileCreatedDate="1900-1-1"
End if

End Property

'取檔案大小
Public Property Get FileAttributes()
If fileExist Then
Dim f,Str
Set f = o_Fso.GetFile(s_FileName)
Select Case f.attributes
Case 0 Str="普通檔案。沒有設定任何屬性。 "
Case 1 Str="唯讀檔案。可讀寫。 "
Case 2 Str="隱藏檔案。可讀寫。 "
Case 4 Str="系統檔案。可讀寫。 "
Case 16 Str="檔案夾或目錄。唯讀。 "
Case 32 Str="上次備份後已更改的檔案。可讀寫。 "
Case 1024 Str="連結或捷徑。唯讀。 "
Case 2048 Str=" 壓縮檔。唯讀。"
End Select
FileAttributes=Str
Else
FileAttributes="檔案不存在或不可預測地屬性"
End if

End Property

Public Function ShowFileAccessInfo(InfoType)
'//功能:顯示檔案建立時資訊
'//形參:檔案名稱,資訊類別
'// 1 -----建立時間
'// 2 -----上次訪問時間
'// 3 -----上次修改時間
'// 4 -----檔案路徑
'// 5 -----檔案名稱
'// 6 -----檔案類型
'// 7 -----檔案大小
'// 8 -----父目錄
'// 9 -----根目錄
'判斷參數是否是數字
If fileExist Then
Dim f, s
Set f = o_Fso.GetFile(s_FileName)
Select Case InfoType
Case 1
s = f.DateCreated '// 1 -----建立時間
Case 2
s = f.DateLastAccessed '// 2 -----上次訪問時間
Case 3
s = f.DateLastModified '// 3 -----上次修改時間
Case 4
s = f.Path '// 4 -----檔案路徑
Case 5
s = f.Name '// 5 -----檔案名稱
Case 6
s = f.Type '// 6 -----檔案類型
Case 7
s = f.Size '// 7 -----檔案大小
Case 8
s = f.ParentFolder '// 8 -----父目錄
Case 9
s = f.RootFolder '// 9 -----根目錄
End Select
ShowFileAccessInfo=s
Else
ShowFileAccessInfo="檔案不存在:("
End if

End Function

Private Function fileExist
fileExist = True
'檢查檔案路徑

'檢查檔案是否存在
If Not(o_Fso.FileExists(s_FileName)) Then
fileExist = False
End If
End Function

Private Function showErr(s_Msg)
Response.Write(s_Msg)
Set Fso=Nothing
Response.End()
End Function

Public Function CreateTxtFile()
'//建立空檔案
If fileExist Then
showErr "檔案已經存在:("
Else
Dim f
Set f=o_Fso.CreateTextFile(s_FileName)
f.Close
End If
End Function

Public Function WriteTxtFile(TextStr,WriteORAppendType)
Const ForReading = 1, ForWriting = 2 , ForAppending = 8
Dim f, m
Select Case WriteORAppendType
Case 1: '檔案進行寫操作
Set f = o_Fso.OpenTextFile(s_FileName, ForWriting, True)
f.Write TextStr
f.Close
If Not fileExist Then
showErr "檔案寫入失敗"
End if
Case 2: '檔案末尾進行寫操作
If fileExist Then
Set f = o_Fso.OpenTextFile(s_FileName, ForAppending)
f.Write TextStr
f.Close
Else
showErr "檔案追加失敗"
End if
End Select
End Function

Public Function ReadTxtFile()
Const ForReading = 1, ForWriting = 2
Dim f, m
If fileExist Then
Set f = o_Fso.OpenTextFile(s_FileName, ForReading)
m = f.ReadLine
'm = f.ReadAll
'f.SkipLine
ReadTxtFile = m
f.Close
Else
ReadTxtFile = "檔案讀取失敗-檔案不存在:("
End if
End Function

Public Function DeleteFile()
If fileExist Then
o_Fso.DeleteFile(s_FileName)
Else
showErr "檔案已經不存在:("
End if
End Function

Public Function CopyFile()
'//功能:源檔案存在時,才能對檔案進行複製,目的檔案無影響
'//SourceFile=s_FileName
If fileExist Then
Dim MyFile
If s_FileName=s_DestinationFile Then
showErr "禁止一相同檔案名稱拷貝到同一目錄下:("
End IF
Set MyFile = o_Fso.GetFile(s_FileName)
MyFile.Copy (s_DestinationFile)
Else
showErr "檔案已經不存在"
End if

End Function

Public Function MoveFile()
If fileExist Then
Dim SourceFile
If s_FileName=s_DestinationFile Then
showErr "禁止一相同檔案名稱移動到同一目錄下:("
End IF
o_Fso.MoveFile s_FileName,s_DestinationFile
Else
showErr "檔案已經不存在:("
End If

End Function

'===============================================目錄操作

'設定目錄名
Public Property Let FolderName(Byval Values)
s_FolderName=Values
'checkFolder '檢查檔案
End Property

'設定目標目錄名
Public Property Let DestinationFolder(Byval Values)
s_DestinationFolder=Values
End Property

'獲得目錄大小

'獲得目錄建立資訊

'判斷目錄是否存在

'建立目錄

'刪除目錄

'拷貝目錄

'移動目錄

'重新命名目錄

'===============================================遍曆操作

End Class

Set myFile=new Cls_FSO
myFile.FileName=Server.MapPath("file.txt")
'myFile.CreateTxtFile()
myFile.WriteTxtFile "Test",2
myFile.DestinationFile=Server.MapPath("file2.txt")
myFile.CopyFile()
'myFile.FileName=Server.MapPath("file2.txt")
'myFile.DeleteFile()
response.Write(myFile.FileSize)
for i =0 to 8
response.Write(myFile.ShowFileAccessInfo(i) )
response.Write("<br>")
next

%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.