首页  >  文章  >  后端开发  >  Asp 对文件操作问题总结

Asp 对文件操作问题总结

怪我咯
怪我咯原创
2017-03-30 13:56:281087浏览
<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                '//        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("
")
next

%>


以上是Asp 对文件操作问题总结的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn