Various types of operations such as files, directories, text files, etc.

Source: Internet
Author: User
Tags chr file copy file size parent directory root directory

One uses the FSO class, the previous article also mentioned. Here are a few more examples.

<%
Dim Myfilefolder
Set Myfilefolder = New filefoldercls
' Response.Write myfilefolder.moveafile ("F:\123\4561.exe", "F:\123\4562.txt")
' Response.Write Myfilefolder.moveafolder ("f:\456", "f:\ditg\456")
' Response.Write Myfilefolder.showfilesystemtype ("I:\")
' Response.Write myfilefolder.copyafile ("F:\123\4562.txt", "F:\123\4563.txt")
''!!!!
' Response.Write Myfilefolder.copyafolder ("f:\123\", "f:\789\")
' Response.Write myfilefolder.showfolderlist ("F:\DITG")
' Response.Write myfilefolder.showfilelist ("f:\123\123")
' Response.Write myfilefolder.deleteafile ("F:\123\4562.txt")
' Response.Write Myfilefolder.deleteafolder ("f:\456\")
' Response.Write Myfilefolder.createfolderdemo ("f:\147\")
' Response.Write myfilefolder.getfilesize ("F:\123\4563.txt")
' Response.Write myfilefolder.getfoldersize ("F:\123\123.txt")
' Response.Write myfilefolder.showfileaccessinfo ("F:\123\123.txt", 1) & "<br>"
' Response.Write myfilefolder.showfileaccessinfo ("F:\123\123.txt", 2) & "<br>"
' Response.Write myfilefolder.showfileaccessinfo ("F:\123\123.txt", 3) & "<br>"
' Response.Write myfilefolder.showfileaccessinfo ("F:\123\123.txt", 4) & "<br>"
' Response.Write myfilefolder.showfileaccessinfo ("F:\123\123.txt", 5) & "<br>"
' Response.Write myfilefolder.showfileaccessinfo ("F:\123\123.txt", 6) & "<br>"
' Response.Write myfilefolder.showfileaccessinfo ("F:\123\123.txt", 7) & "<br>"
' Response.Write myfilefolder.showfileaccessinfo ("F:\123\123.txt", 8) & "<br>"
' Response.Write myfilefolder.showfolderaccessinfo ("f:\123\", 1) & "<br>"
' Response.Write myfilefolder.showfolderaccessinfo ("f:\123\", 2) & "<br>"
' Response.Write myfilefolder.showfolderaccessinfo ("f:\123\", 3) & "<br>"
' Response.Write myfilefolder.showfolderaccessinfo ("f:\123\", 4) & "<br>"
' Response.Write myfilefolder.showfolderaccessinfo ("f:\123\", 5) & "<br>"
' Response.Write myfilefolder.showfolderaccessinfo ("f:\123\", 6) & "<br>"
' Response.Write myfilefolder.showfolderaccessinfo ("f:\123\", 7) & "<br>"
' Response.Write myfilefolder.showfolderaccessinfo ("f:\123\", 8) & "<br>"
' Response.Write myfilefolder.writetxtfile ("F:\123\cexo.txt", "" ", 1) &" <br> "
' Response.Write myfilefolder.writetxtfile ("F:\123\cexo.txt", "CEXOWXFDW", 1) & "<br>"
"Response.Write Myfilefolder.writetxtfile (" F:\123\cexo.txt ", Chr) &AMP;CHR (10)
& "CEXOWXFDW", 2) & "<br>"
' Response.Write myfilefolder.readtxtfile ("F:\123\cexo.txt") & "<br>"
Response.Write myfilefolder.displayleveldepth ("F:\ditg\ditg\bumen\images") & "<br>"
Class Filefoldercls
Public FSO
Private Sub Class_Initialize ()
Set fso = CreateObject ("Scripting.FileSystemObject")
End Sub
Private Sub Class_Terminate ()
Set FSO = Nothing
End Sub
'//================================== file Operation ==================================
' Take file size
Function GetFileSize (FileName)
'//function: File size
'//formal parameter: filename
'//Return Value: Success for file size, failure to-1
''//
Dim F
If reportfilestatus (FileName) = 1 Then
Set f = fso. Getfile (FileName)
GetFileSize = F.size
Else
GetFileSize =-1
End If
End Function
' File deletion
Function Deleteafile (filespec)
'//function: File deletion
'//formal parameter: filename
'//Return Value: Success is 1, failure is-1
''//
If reportfilestatus (filespec) = 1 Then
Fso. DeleteFile (filespec)
Deleteafile = 1
Else
Deleteafile =-1
End If
End Function
' Show file list
Function showfilelist (FOLDERSPEC)
'//function: Displays all files in this directory when the directory exists
'//formal parameter: directory name
'//Return Value: Success for file list, failed-1
''//
Dim F, F1, FC, S
If reportfolderstatus (Folderspec) = 1 Then
Set f = fso. GetFolder (FOLDERSPEC)
Set FC = F.files
For each F1 in FC
s = S & f1.name
s = S & "|"
Next
Showfilelist = S
Else
Showfilelist =-1
End If
End Function
''!!!
' File copy
Function Copyafile (Sourcefile,destinationfile)
'//function: When the source file exists, the file can be copied, the destination file has no effect
'//Parameter: source file, destination file
'//Return Value: Success is 1, failure is-1
''//
Dim MyFile
If reportfilestatus (sourcefile) = 1 Then
Set MyFile = fso. GetFile (sourcefile)
Myfile.copy (Destinationfile)
Copyafile = 1
Else
Copyafile =-1
End If
End Function
' File move
' Response.Write moveafile ("F:\123\4561.exe", "F:\123\4562.txt")
Function Moveafile (Sourcefile,destinationfile)
'//function: To move a file if the destination file does not exist when the source file exists
'//Parameter: source file, destination file
'//Return Value: Success is 1, failure is-1
''//
If reportfilestatus (sourcefile) =1 and Reportfilestatus (destinationfileorpath) =
-1 Then
Fso. MoveFile Sourcefile,destinationfileorpath
Moveafile = 1
Else
Moveafile =-1
End If
End Function
"Does the file exist?"
' Response.Write reportfilestatus ("g:oft\delphi\my_pro\ code library. exe")
Function Reportfilestatus (FileName)
'//function: To determine whether a file exists
'//formal parameter: filename
'//Return Value: Success is 1, failure is-1
''//
Dim msg
msg =-1
If (FSO). FileExists (FileName)) Then
msg = 1
Else
msg =-1
End If
Reportfilestatus = Msg
End Function
' File creation date
' Response.Write showdatecreated ("g:oft\delphi\my_pro\ code library. exe")
' Response.Write showdatecreated (' g:oft\delphi\my_pro\ code library. exe ')
Function showdatecreated (filespec)
'//function: File creation date
'//formal parameter: filename
'//Return Value: Success: File creation date, failed:-1
''//
Dim F
If reportfilestatus (filespec) = 1 Then
Set f = fso. GetFile (filespec)
showdatecreated = f.datecreated
Else
showdatecreated =-1
End If
End Function
' File properties
' Response.Write getattributes (' g:oft\delphi\my_pro\ code library. exe ')
Function getattributes (FileName)
'//function: Display file properties
'//formal parameter: filename
'//Return Value: Success: File properties, failed:-1
''//
Dim F,str
If reportfilestatus (FileName) = 1 Then
Set f = fso. GetFile (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= "hides the file. can read and write. "
Case 4 Str= "system files. can read and write. "
Case str= folder or directory. Read-only. "
Case str= The file that has changed since the last backup. can read and write. "
Case 1024 str= "link or shortcut. Read-only. "
Case 2048 str= "compressed file. Read-only. "
End Select
GetAttributes = Str
Else
GetAttributes =-1
End If
End Function
' Last visit/Last modification time
' Response.Write showfileaccessinfo (' g:oft\delphi\my_pro\ code library. exe ')
Function Showfileaccessinfo (Filename,infotype)
'//function: Show information when file is created
'//Parameter: file name, information category
'//1-----creation time
'//2-----Last access time
'//3-----Last modification time
'//4-----file path
'//5-----file name
'//6-----file type
'//7-----File Size
'//8-----Parent Directory
'//9-----root directory
'//Return value: Message created successfully for file, failed:-1
''//
Dim F, S
If reportfilestatus (FileName) = 1 Then
Set f = fso. GetFile (FileName)
Select Case InfoType
Case 1 s = f.datecreated '//1-----
Creation time
Case 2 s = f.datelastaccessed '//2-----Last visit
Time
Case 3 S = f.datelastmodified '//3-----Last Modified
Time
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 '//8-----
root directory
End Select
Showfileaccessinfo = S
ELse
Showfileaccessinfo =-1
End If
End Function
"Write a text file
Function Writetxtfile (Filename,textstr,writeorappendtype)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim F, M
Select Case Writeorappendtype
Case 1: ' File for write operation
Set f = fso. OpenTextFile (FileName, ForWriting, True)
F.write Textstr
F.close
If reportfilestatus (FileName) = 1 Then
Writetxtfile = 1
Else
Writetxtfile =-1
End If
Case 2: ' Write at end of file '
If reportfilestatus (FileName) = 1 Then
Set f = fso. OpenTextFile (FileName, ForAppending)
F.write Textstr
F.close
Writetxtfile = 1
Else
Writetxtfile =-1
End If
End Select
End Function
' Read the text file
Function Readtxtfile (FileName)
Const ForReading = 1, ForWriting = 2
Dim F, M
If reportfilestatus (FileName) = 1 Then
Set f = fso. OpenTextFile (FileName, ForReading)
m = F.readline
"M = F.readall
"' F.skipline
Readtxtfile = m
F.close
Else
Readtxtfile =-1
End If
End Function
"Create a text file
'//================================== directory Operation ==================================
' Directory Size
Function getfoldersize (FolderName)
'//function: Size of directory
'//formal parameter: directory name
'//Return Value: Success is directory size, failed to-1
''//
Dim F
If reportfolderstatus (FolderName) = 1 Then
Set f = fso. GetFolder (FolderName)
Getfoldersize = F.size
Else
Getfoldersize =-1
End If
End Function
"Folder created by
Function Createfolderdemo (FolderName)
'//function: Created folder
'//formal parameter: directory name
'//Return Value: Success is 1, failure is-1
''//
Dim F
If reportfolderstatus (Folderspec) = 1 Then
Createfolderdemo =-1
Else
Set f = fso. CreateFolder (FolderName)
Createfolderdemo = 1
End If
End Function
''!!!
' Directory deletion
Function Deleteafolder (FOLDERSPEC)
'//function: Directory deletion
'//formal parameter: directory name
'//Return Value: Success is 1, failure is-1
''//
Response.Write Folderspec
If reportfolderstatus (Folderspec) = 1 Then
Fso. DeleteFolder (FOLDERSPEC)
Deleteafolder = 1
Else
Deleteafolder =-1
End If
End Function
' Show directory list
Function showfolderlist (FOLDERSPEC)
'//function: Displays all subdirectories under this directory when the directory exists
'//formal parameter: directory name
'//Return value: Successfully a subdirectory list, failed-1
''//
Dim F, F1, FC, S
If reportfolderstatus (Folderspec) = 1 Then
Set f = fso. GetFolder (FOLDERSPEC)
Set FC = F.subfolders
For each F1 in FC
s = S & f1.name
s = S & "|"
Next
Showfolderlist = S
Else
Showfolderlist =-1
End If
End Function
''!!!!
' Directory replication
Function Copyafolder (Sourcefolder,destinationfolder)
'//function: When the source directory exists, the directory can be replicated, the destination directory does not affect
'//Formal parameters: Source directory, Destination directory
'//Return Value: Success is 1, failure is-1
''//
"Dim MyFolder
' If reportfolderstatus (sourcefolder) = 1 and Reportfolderstatus
(destinationfolder) =-1 Then
' Set MyFolder = fso. GetFolder (Sourcefolder)
Fso. CopyFolder Sourcefolder,destinationfolder
Copyafolder = 1
"Else
Copyafolder =-1
' End If
End Function
' Directory to move
Function Moveafolder (Sourcepath,destinationpath)
'//function: The directory can be moved if the destination directory does not exist when the source directory exists
'//Formal parameters: Source directory, Destination directory
'//Return Value: Success is 1, failure is-1
''//
If reportfolderstatus (SourcePath) =1 and Reportfolderstatus (DestinationPath) =0
Then
Fso. MoveFolder SourcePath, DestinationPath
Moveafolder = 1
Else
Moveafolder =-1
End If
End Function
"To determine whether a directory exists
' Response.Write reportfolderstatus ("g:oft\delphi\my_pro\")
Function Reportfolderstatus (FLDR)
'//function: To determine whether a directory exists
'//Formal parameters: Table of Contents
'//Return Value: Success is 1, failure is-1
''//
Dim msg
msg =-1
If (FSO). FolderExists (FLDR)) Then
msg = 1
Else
msg =-1
End If
Reportfolderstatus = Msg
End Function
' Directory creation time information
Function Showfolderaccessinfo (Foldername,infotype)
'//function: Display directory creation information
'//formal parameter: directory name, information category
'//1-----creation time
'//2-----Last access time
'//3-----Last modification time
'//4-----directory path
'//5-----Directory Name
'//6-----Directory Type
'//7-----Directory Size
'//8-----Parent Directory
'//9-----root directory
'//Return value: Message successfully created for directory, failed:-1
''//
Dim F, S
If reportfolderstatus (FolderName) = 1 Then
Set f = fso. GetFolder (FolderName)
Select Case InfoType
Case 1 s = f.datecreated '//1-----
Creation time
Case 2 s = f.datelastaccessed '//2-----Last visit
Time
Case 3 S = f.datelastmodified '//3-----Last Modified
Time
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
Showfolderaccessinfo = S
ELse
Showfolderaccessinfo =-1
End If
End Function
Function displayleveldepth (PATHSPEC)
Dim F, N, Path
Set f = fso. GetFolder (PATHSPEC)
If F.isrootfolder Then
displayleveldepth = "The folder specified is the root folder. "&rootfolder
Else
Do Until F.isrootfolder
Path = path & F.name & "<br>"
Set f = F.parentfolder
n = n + 1
Loop
displayleveldepth = "The folder specified is a folder with a nested level of" & N & ". <br> "&
Path
End If
End Function
'//================================== disk Operation ==================================
"Does the drive exist?"
' Response.Write reportdrivestatus ("C:\")
Function Reportdrivestatus (DRV)
'//function: Determine if the disk exists
'//Formal parameters: Disk
'//Return Value: Success is 1, failure is-1
''//
Dim msg
msg =-1
If FSO. Driveexists (DRV) Then
msg = 1
Else
msg =-1
End If
Reportdrivestatus = Msg
End Function
'--------available return types include FAT, NTFS, and CDFS.
' Response.Write Showfilesystemtype ("C:\")
Function Showfilesystemtype (DRVSPEC)
'//function: Disk type
'//formal parameter: disk name
'//Return Value: Success is type: FAT, NTFS, and CDFS, failed:-1
''//
Dim D
If reportdrivestatus (Drvspec) = 1 Then
Set d = fso. Getdrive (DRVSPEC)
Showfilesystemtype = D.filesystem
ELse
Showfilesystemtype =-1
End If
End Function
End Class
%>

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.