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

Asp summary of file operation problems

Mar 30, 2017 pm 01:56 PM

<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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

File operations in PHP8.0: file monitoring File operations in PHP8.0: file monitoring May 14, 2023 pm 02:21 PM

With the continuous development of web applications, PHP has become one of the most important programming languages ​​in web development. As an extremely flexible programming language, each version of PHP brings new features and optimizations to meet different needs and application scenarios. In the PHP8.0 version, a very practical file operation function has been added, namely file monitoring. This function is very suitable for application scenarios that require monitoring and processing of file changes, such as file backup, file synchronization, log monitoring, etc. This article will take you

Can I delete gho files? Can I delete gho files? Feb 19, 2024 am 11:30 AM

A gho file is an image file created by NortonGhost software and used to back up and restore the operating system and data. In some cases, you can delete gho files, but do so with caution. This article will introduce the role of gho files, precautions for deleting gho files, and how to delete gho files. First, let's understand the role of gho files. A gho file is a compressed system and data backup file that can save an image of an entire hard disk or a specific partition. This kind of backup file is usually used for emergency recovery

How to safely read and write files using Golang? How to safely read and write files using Golang? Jun 06, 2024 pm 05:14 PM

Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

Go Programming Tips: Deleting Contents from a File Go Programming Tips: Deleting Contents from a File Apr 04, 2024 am 10:06 AM

The Go language provides two methods to clear file contents: using io.Seek and io.Truncate, or using ioutil.WriteFile. Method 1 involves moving the cursor to the end of the file and then truncating the file, method 2 involves writing an empty byte array to the file. The practical case demonstrates how to use these two methods to clear content in Markdown files.

How to fix: Java file operation error: File write failed How to fix: Java file operation error: File write failed Aug 26, 2023 pm 09:13 PM

How to solve: Java file operation error: File writing failed. In Java programming, you often encounter the need for file operations, and file writing is one of the important functions. However, sometimes we encounter file writing failure errors, which may prevent the program from running properly. This article will describe some common causes and solutions to help you solve this type of problem. Wrong path: A common problem is wrong file path. When we try to write a file to the specified path, if the path does not exist or the permissions are insufficient, the file will be written.

Learn the file operation functions in Go language and implement the encryption, compression, upload and download functions of files Learn the file operation functions in Go language and implement the encryption, compression, upload and download functions of files Jul 29, 2023 pm 10:37 PM

Learn the file operation functions in Go language and implement the encryption, compression, upload and download functions of files. Go language is an open source statically typed programming language. It is widely popular in the development field for its efficient performance and concise syntax. The standard library of the Go language provides a wealth of file operation functions, making it very simple to read and write files, encrypt and compress them, upload and download them. This article will introduce how to use the file operation functions in the Go language to implement the functions of encrypting, compressing, uploading and downloading files. First, we need to import the relevant three

PHP file operation example: reading CSV file PHP file operation example: reading CSV file Jun 20, 2023 am 11:42 AM

PHP is a popular programming language widely used in web development. In web applications, file operations are a basic and common function. This article will explain how to use PHP to read a CSV file and display it in an HTML table. CSV is a common file format used to import tabular data into spreadsheet software such as Excel. CSV files usually consist of many lines, each line consisting of comma separated values. The first line usually contains column headers, which describe the meaning of each column value. Here we will use PHP

How to insert content at a specified location in a file using C++? How to insert content at a specified location in a file using C++? Jun 04, 2024 pm 03:34 PM

In C++, use the ofstream class to insert content at a specified location in a file: open the file and locate the insertion point. use

See all articles