首頁  >  文章  >  後端開發  >  Asp 對文件操作問題總結

Asp 對文件操作問題總結

怪我咯
怪我咯原創
2017-03-30 13:56:281088瀏覽
<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")="" then
		Errmsg=Errmsg+"<br>"+"<li>本页面为管理员专用,请<a href=admin_index.asp target=_top>登陆</a>后进入。<br><li>您没有管理本页面的权限。"
		call dvbbs_error()
		response.end
	
	end if
	on 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     Case 5
    s = f.Name   ##    Case 6
    s = f.Type                '//        6 -----檔案類型
   Case 7
    s = f    Case 8
    s = f. ParentFolder        '//        8  -----父目錄
   Case 9
    s = f.RootFolder    End ##   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

#Func_Function Func



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

# Public Function CreateTxtFile()
'//建立空白檔案

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

Public Function WriteTxtFile(TextStr,WriteORAppendType)
 Const ForReading = 1, ForWriting = 2 , ForAppending = 8
   Case 1 :            '檔案進行寫入作業
           Set f = o_Fso.OpenTextFile(s_FileName, ForWriting, True)# ose
           If Not fileExist Then
               showErr "檔案寫入失敗"
           End if
       Case 2:            '檔案結尾與選擇作業 #    Set f = o_Fso.OpenTextFile(s_FileName, ForAppending)
               f.Write TextStr
          .Close
           Else
               showErr 「 ReadTxtFile()
 Const ForReading = 1, ForWriting = 2
       Dim f, m
       If fileExist Then
           Set f = o_F.OpenTextFile(s_FileName, ForRe ##            'm =  f.ReadAll
           ' f.SkipLine
           ReadTxtFile = m
           f.Close
      Else## c f.Close##       End if
End Function

# Public Function DeleteFile()
       If fileExist Then
  o_Fso.DeleteFile(s_FileName)
 Else
  showErr "檔案已經不存在:("#p
## 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)#. 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_Fstination_Destin o_Fstination_De o_Fstination_De o_Fstination。 #  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("
")
next

%>

# Asp 對文件操作問題總結

#

以上是Asp 對文件操作問題總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn