Home > Backend Development > C#.Net Tutorial > Asp summary of file operation problems

Asp summary of file operation problems

怪我咯
Release: 2017-03-30 13:56:28
Original
1132 people have browsed it
<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>
Copy after login


动网论坛的代码,你参考一下吧
<!--#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>
Copy after login


ASP--文件操作类 设计版
Copy after login


<%
'====================================================
'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 ----- file path
case 5
s = f.name' // 5 ----- file name
case 6
s = f.type '// 6 ----- File type
Case 7
s = f.size' // 7 ------ The file size
case 8
s = f. ParentFolder '// 8 -----Parent Directory
Case 9
s = f.RootFolder '// 9 9 -----Root Directory
End Select
ShowFileAccessInfo=s
Else
ShowFileAccessInfo="File does not exist:("
End if

End Function

Private Function fileExist
fileExist = True
'Check file path

'Check if the file exists
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()
'//Create an empty file
If fileExist Then
showErr "The file already exists:("
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
         ; ##                   showErr "File writing Failed"
End if
Case 2: "Write operation at the end of the file
If fileExist Then
Set f = o_Fso.OpenTextFile(s_FileName, ForAppending)
​ ​ ​ ​ ​ ​.Close
                                                                  using using using using ’         out through out through out through out out through out out through out       ’’’''  ‐     ‐‐ ‐‐‐‐w _ = 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
                              ‐   ‐                 ’s     ’s ’s ‐ ‐ ‐   ‐   ‐ ‐‐ ‐ readTxtFile = ​                                                                                 ’ s ’ s ’ s ’ s ’ s ’ s ‐ ‐ ‐ ‐ ft . f. ## Public Function DeleteFile()
If fileExist Then
o_Fso.DeleteFile(s_FileName)
Else
showErr "The file no longer exists:("
End if
End Function

Public Function CopyFile()
'//Function: The file can be copied only when the source file exists, and the destination file has no effect
'//SourceFile=s_FileName
If fileExist Then
Dim MyFile
If s_FileName=s_DestinationFile Then
showErr "Prohibit copying the same file name to the same directory:("
End IF
Set MyFile = o_Fso.GetFile(s_FileName)
MyFile. Copy (s_DestinationFile)
Else
showErr "The file no longer exists"
End if

End Function

Public Function MoveFile()
If fileExist Then
Dim SourceFile
If s_FileName=s_DestinationFile Then
showErr "Prohibit moving a file with the same name to the same directory:("
End IF
o_Fso.MoveFile s_FileName,s_DestinationFile
Else
showErr "The file no longer exists:("
End If

End Function

'================ =============================== Directory operations

'Set directory name
Public Property Let FolderName (Byval Values)
s_FolderName=Values
'checkFolder 'Check the file
End Property

'Set the target directory name
Public Property Let DestinationFolder(Byval Values)
s_DestinationFolder= Values
End Property

'Get directory size

'Get directory creation information

'Determine whether directory exists

'Create directory

'Delete directory

'Copy directory

'Move directory

'Rename directory

'========== ====================================Traversal operation

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

%>


The above is the detailed content of Asp summary of file operation problems. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template