A summary of ASP's operation on files

Source: Internet
Author: User
Tags microsoft frontpage


Dynamic Network Forum Code, you refer to it <!--#include file =conn.asp--><!--#include file= "inc/const.asp"--><title><%= Forum_info (0)%>--Administration page </title><!--#include file=inc/forum_css.asp--><meta name=generator Content = "Microsoft FrontPage 3.0" Charset=gb2312><body <%=forum_body (one)%>><%if not master or session ("flag ") =" thenerrmsg=errmsg+ "<br>" + "<li> this page is for administrators only, please <a href=admin_index.asp target=_top> login </a> After entering. <br><li> you do not have permission to manage this page. "Call Dvbbs_error () Response.endend Ifon Error Resume Next Sub showspaceinfo (drvpath) Dim fso,d,size,showsize Set Fso=serv Er.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&G t;1024 then size= (size/1024) Showsize=formatnumber (size,2) & "MB" End If if size>1024 then size= (size/102 4) 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 s  Ize=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& gt; & Showsize & </font> End Sub Function drawbar (drvpath) Dim fso,drvpathroot,d,size,totalsize,barsi Ze 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) Drawspecia     Lbar=barsize End Function%><table align=center cellspacing=1 cellpadding=1 class=tableborder1> <tr> <th height=25> System space usage </th> </th> </tr> <tr> <td class=tablebody1> < Blockquote> <% fsoflag=1 If fsoflag=1 then%> <br> regulatory data footprint: &Lt;img src= "Pic/bar1.gif" Width=<%=drawbar ("Data")%> height=10> <%showspaceinfo ("Data")%><br> <br> Backup Data footprint:  height=10> <% Showspaceinfo ("DataBackup")%><br><br> Program Files occupy space:  height=10> <%showspecialspaceinfo ("program")%><br><br> mood picture occupy space:  height=10> <%showspaceinfo ("Face")%><br>< br> system picture occupy space:  height=10> <%showspaceinfo ("pic")% ><br><br> upload avatar occupy space:  height=10> <%showspaceinfo ("Uploadface")%><br><br> upload image occupy space:  height=10> <%showspaceinfo ("UploadImages")%><br><br> system Space Total: &Lt;br> <%showspecialspaceinfo ("All")%> <% Else Response.Write "<br><li> This feature has been turned off" End If%> </blockquote> </td> </tr></table& Gt


asp--file Operation class Design version


<%
'====================================================
' Version:aspfile 1.0
' data:2006.6.20
' CopyRight: (C) 2006 evan.cn All rights Reserved
'====================================================
' File Operation class
' File name
' File size
' File creation
' filename--file name
' destinationfile--target file name
' filesize--Get file size
' filecreateddate--Get file creation date
' fileattributes--get file attributes
' Showfileaccessinfo (infotype)--Get file attributes
' Createtxtfile ()--Create a text file
' Writetxtfile (textstr,writeorappendtype) = = Write text file
' Readtxtfile ()--read a text file
' DeleteFile ()--delete file
' CopyFile ()--Copy file
' MoveFile ()--Move file
'
'
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

' =============================================== file operations

' Set file name
Public Property Let FileName (Byval Values)
S_filename=values
' Checkfile ' Check file
End Property

' Set the target file name
Public Property Let Destinationfile (Byval Values)
S_destinationfile=values
End Property


' Fetch file size
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

' Fetch files
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

' Fetch file size
Public Property Get FileAttributes ()
If FileExist Then
Dim F,str
Set f = o_fso.getfile (S_filename)
Select Case F.attributes
Case 0 str= "normal file. No properties are set. "
Case 1 Str= "read-only file. can read and write. "
Case 2 str= "hidden file. can read and write. "
Case 4 str= "System file. can read and write. "
Case str= "folder or directory. Read-only. "
Case str= "files that have changed since the last backup." can read and write. "
Case 1024x768 str= "link or shortcut. Read-only. "
Case 2048 str= "compressed file. Read-only. "
End Select
Fileattributes=str
Else
fileattributes= "file does not exist or is not predictable property"
End if

End Property

Public Function Showfileaccessinfo (infotype)
'//function: Display file creation information
'//Parameter: file name, information category
'//1-----creation time
'//2-----Last access time
'//3-----Last Modified
'//4-----file path
'//5-----file name
'//6-----file type
'//7-----File Size
'//8-----Parent Directory
'//9-----root directory
' Determine if the parameter is a number
If FileExist Then
Dim F, S
Set f = o_fso.getfile (S_filename)
Select Case InfoType
Case 1
s = f.datecreated '//1-----creation time
Case 2
s = f.datelastaccessed '//2-----Last access time
Case 3
s = f.datelastmodified '//3-----Last Modified
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-----File Size
Case 8
s = F.parentfolder '//8-----Parent Directory
Case 9
s = F.rootfolder '//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 "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
Case 1: ' File for write operation
Set f = o_fso.opentextfile (S_filename, ForWriting, True)
F.write Textstr
F.close
If not FileExist Then
Showerr "File Write Failed"
End if
Case 2: ' Write at end of file '
If FileExist Then
Set f = o_fso.opentextfile (S_filename, ForAppending)
F.write Textstr
F.close
Else
Showerr "File append failed"
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 = "File read failed-file does not exist: ("
End if
End Function

Public Function DeleteFile ()
If FileExist Then
O_fso.deletefile (S_filename)
Else
Showerr "File no longer exists: ("
End if
End Function

Public Function CopyFile ()
'//function: When the source file exists, the file can be copied, the destination file has no effect
'//sourcefile=s_filename
If FileExist Then
Dim MyFile
If S_filename=s_destinationfile Then
Showerr "Prohibit a copy of the same file name to the same directory: ("
End IF
Set MyFile = O_fso.getfile (s_filename)
Myfile.copy (S_destinationfile)
Else
Showerr "File no longer exists"
End if

End Function

Public Function MoveFile ()
If FileExist Then
Dim sourcefile
If S_filename=s_destinationfile Then
Showerr "Prohibit a same file name from being moved to the same directory: ("
End IF
O_fso.movefile S_filename,s_destinationfile
Else
Showerr "File no longer exists: ("
End If

End Function

' =============================================== directory Operations

' Set directory Name
Public Property Let FolderName (Byval Values)
S_foldername=values
' Checkfolder ' Check 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 if the 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 ("<br>")
Next

%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.