FileSystemObject (fso) sample code

zhaozj2021-02-11  199

FILESYSTEMOBJECT sample code

The sample code described in this section provides a real example to demonstrate

Many features available in FileSystemObject object mode. This code shows how to use all functions of the object mode and how to use these features in your own code.

Please note that since this code is extremely general, it is necessary to run the code to run on your machine, some other code and small changes may be required. These changes are necessary because of the Active Server Pages and Windows Scripting Host, different methods are used for input and output to users.

To run the code on the Active Server Pages, follow these steps:

Create a standard web page, the suffix name is .asp. Copy the following sample code to the file between the ... tag. Turn all code wrappers to <% ...%>. Move the OPTION EXPLICIT statement from the current position to the top of the HTML page, even before the starts tag. Place the <% ...%> tag around the Option Explicit statement to ensure that it runs on the server side. Add the following code to the end of the sample code:

SUB Print (x)

Response.write "

 "

Response.write X

Response.write " "

End Sub

Main

The previous code adds a printing process that will run on the server side, but display the result of the client. To run the code on Windows Scripting Host, add the following code to the end of the sample code:

SUB Print (x)

WScript.echo X

End Sub

Main

Below is the sample code:

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''

'

'FileSystemObject sample code

'

'Copyright 1998 Microsoft Corporation. all rights reserved.

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' 'Option Explicit

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

'

'For code quality:

'

'1) The following code has a number of string operations, with the "&" operator to connect the short string together. due to

The 'string connection is time, so this is a low efficiency write code method. Anyway, it is

'A very good maintenance write code method, and use this method here because the program is executed

'A large number of disk operations, while disk operations are much slower than the memory operations required for the connection string.

'Remember this is a demonstration code, not product code.

'

'2) Use "Option Explicit" because the variables that have been accessed are more than the variable that has not been declared.

'Something is slightly. It also prevents errors in the code, for example, misunderstanding DriveTypecdrom

'Became DriveTypecdorm.

'

'3) In order to make the code more readable, there is no error handling in this code. Although the prevention measures have been taken to ensure the code

'There is no error in normal case, but the file system is unpredictable. In the product code, use

'On Error Resume Next and Err objects capture possible errors.

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

'

'Some easy to obtain global variables

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

DIM TABSTOP

DIM Newline

Const testDrive = "c"

Const testfilepath = "c: / test"

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

'

'Returned by Drive.DriveType

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' Const DriveTyperemovable = 1

Const drivetypefixed = 2

Const drivetypenetwork = 3

Const driveTypecdrom = 4

Const drivetyperamdisk = 5

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

'

'Returned by file.attributes

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

Const filettrnormal = 0

Const filettrreadonly = 1

Const filettrhidden = 2

Const fileattrstem = 4

Const filettrvolume = 8

Const filettrdirectory = 16

Const filettrarchive = 32

Const fileAttralias = 64

Const fileatTrcompressed = 128

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''

'Constants used to open the file

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

Const openfileforreading = 1

Const openfileforwriting = 2

Const openfileforaplending = 8

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

'

'ShowdriveType

'

'Purpose:

'

'Generate a string to describe the drive type for a given DRIVE object.

'

'Demonstration below

'

'- Drive.DriveType

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

Function ShowdriveType (Drive) DIM S

Select Case Drive.DriveType

Case DriveTyperemovable

S = "removeable"

Case driveTypefixed

S = "fixed"

Case DriveTyPenetwork

S = "network"

Case DriveTypecdrom

S = "CD-ROM"

Case DriveTyperamdisk

S = "RAM Disk"

Case Else

S = "unknown"

End SELECT

ShowdriveType = S

END FUNCTION

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

'

'ShowFileAttr

'

'Purpose:

'

'Generate a string to describe the properties of the file or folder.

'

'Demonstration below

'

'- file.attributes

'- Folder.attributes

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

Function ShowFileAttr (file) 'file can be a file or folder

DIM S

DIM Attr

Attr = file.attributes

IF attr = 0 THEN

ShowfileAttr = "Normal"

EXIT FUNCTION

END IF

If attr and fileattrdirectory the s = s & "directory"

If attr and fileattrreadonly the s = s & "read-only"

If attr and fileattrhidden dam = s & "hidden"

IF attr and fileattrsystem kiln s = s & "system" if attr and fileattrvolume damhen s = s & "volume"

IF attr and fileattrarchive the s = s & "archive"

If attr and fileattralias the s = s & "alias"

If attr and fileattrcompressed then s = s & "commitsed"

ShowfileAttr = S

END FUNCTION

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

'

'GeneratedRiveInformation

'

'Purpose:

'

'Generate a string to describe the current status of the available drive.

'

'Demonstration below

'

'- FileSystemObject.drives

'- ITERATING The Drives Collection

'- Drives.count

'- Drive.availableSpace

'- Drive.Driveletter

'- Drive.DriveType

'- Drive.FileSystem

'- Drive.Freespace

'- Drive.Indy

'- Drive.path

'- Drive.SerialNumber

'- Drive.ShareName

'- Drive.Totalsize

'- Drive.volumename

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

Function GeneratedRiveInformation (FSO)

DIM Drives

DIM Drive

DIM S

Set drivers = fso.drives

S = "Number of Drives:" & Tabstop & Drives.count & Newline & Newline 'Constructs the first line of the report.

S = S & String (2, Tabstop) & "Drive"

S = S & String (3, TabStop) & "File"

S = S & TabStop & "Total"

S = S & TabStop & "Free"

S = S & TabStop & "Available"

S = S & TabStop & "Serial" & newline

'The second line of construct report.

S = S & "Letter"

S = S & TabStop & "Path"

S = S & TabStop & "Type"

S = S & TabStop & "Ready?"

S = S & TabStop & "Name"

S = S & TabStop & "System"

S = S & TabStop & "Space"

S = S & TabStop & "Space"

S = S & TabStop & "Space"

S = S & TabStop & "Number" & newline

'Dividel.

S = S & String (105, "-") & newline

For Each Drive In Drives

S = s & drive.driveletter

S = S & TabStop & Drive.Path

S = S & TabStop & ShowdriveType (Drive)

S = S & TabStop & Drive.Isready

If drive.isready then

If DriveTyPenetwork = drive.drivetype the

S = S & Tabstop & Drive.ShareName

Else

S = S & TabStop & Drive.Volumename

END IF

S = S & TabStop & Drive.FileSystem

S = S & TabStop & Drive.Totalsize

S = S & TabStop & Drive.Freespace

S = S & TabStop & Drive.availablespace

S = S & TabStop & Hex (Drive.SerialNumber)

END IF

S = S & Newline

NEXT

GeneratedRiveInformation = S

END FUNCTION

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''

'Generatefileinformation

'

'Purpose:

'

'Generate a string to describe the current status of the file.

'

'Demonstration below

'

'- file.path

'- file.name

'- File.Type

'- file.datecreated

'- file.dateLastAccessed

'- file.dateLastModified

'- File.Size

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

Function Generatefileinformation (file)

DIM S

S = Newline & "Path:" & Tabstop & File.Path

S = S & Newline & "Name:" & Tabstop & File.Name

S = S & Newline & "Type:" & Tabstop & File.Type

S = S & Newline & "Attribs:" & TabStop & ShowFileattr (file)

S = S & Newline & "Created:" & TabStop & File.datecreated

S = S & Newline & "Accessed:" & TabStop & File.DateLastAccessed

S = S & Newline & "Modified:" & TabStop & File.DateLastModified

S = S & Newline & "Size" & Tabstop & File.size & newline

Generatefileinformation = send function

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

'

'Generatefolderinformation

'

'Purpose:

'

'Generate a string to describe the current status of the folder.

'

'Demonstration below

'

'- Folder.Path

'- Folder.Name

'- Folder.datecreated

'- Folder.DateLastAccessed

'- Folder.DateLastModified

'- Folder.Size

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

Function GenerateFolderInformation (Folder)

DIM S

S = "PATH:" & Tabstop & Folder.Path

S = S & Newline & "Name:" & tabstop & folder.name

S = S & Newline & "Attribs:" & TabStop & ShowFileAttr (Folder)

S = S & Newline & "Created:" & TabStop & Folder.datecreated

S = S & Newline & "Accessed:" & tabstop & folder.dateLastAccessed

S = S & Newline & "Modified:" & TabStop & Folder.DateLastModified

S = S & Newline & "Size:" & TabStop & Folder.Size & Newline

GenerateFolderinformation = send function

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

'

'GenerateAllFolderInformation

'

'Purpose:

'

'Generate a string to describe a folder and all files and subfolders.

'

'Demonstration below

'

'- Folder.Path

'- Folder.Subfolders

'- folders.count

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

Function GenerateAllFolderInformation (Folder)

DIM S

DIM Subfolders

DIM Subfolder

DIM Files

DIM file

S = "Folder:" & Tabstop & Folder.Path & Newline & Newline

Set files = folder.files

IF 1 = files.count dam

S = s & "there is 1 file" & newline

Else

S = S & "There Are" & files.count & "files" & newline

END IF

IF files.count <> 0 THEN

For Each File in Files

S = s & generatefileinformation (file)

NEXT

END IF

Set Subfolders = folder.subfolders

IF 1 = Subfolders.count Then

S = S & Newline & "There IS 1 Sub Folder" & newline & newline

Else

S = S & Newline & "There Are" & Subfolders.count & "Sub Folders" & newline & newlineend IF

If Subfolders.count <> 0 THEN

For Each Subfolder in Subfolders

S = S & GenerateFolderinformation (Subfolder)

NEXT

S = S & Newline

For Each Subfolder in Subfolders

S = S & GenerateAllFolderInformation (Subfolder)

NEXT

END IF

GenerateAllFolderinformation = S

END FUNCTION

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

'

'Generatetestinformation

'

'Purpose:

'

'Generate a string to describe the current state of the C: / Test folder and all files and subfolders.

'

'Demonstration below

'

'- FileSystemObject.driveexists

'- FileSystemObject.foldeRexists

'- FileSystemObject.getFolder

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

Function GenerateTestinformation (FSO)

DIM Testfolder

DIM S

IF not fso.driveexists (testdrive) THEN EXIT FUNCTION

IF not fso.folderexists (testfilepath) THEN EXIT FUNCTION

Set testfolder = fso.getfolder (TestFilePath)

GenerateTestinformation = GenerateAllFolderInformation (TestFolder)

End Function'''''''''''''''''''''''''''''''''''''''''''''''' '' '' '' '' '' '' '' '' '' ''

'

'Deletetestdirectory

'

'Purpose:

'

'Clean up the Test directory.

'

'Demonstration below

'

'- FileSystemObject.getFolder

'- FileSystemObject.deletefile

'- FileSystemObject.deletefolder

'- Folder.delete

'- file.delete

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

Sub deletetestDirectory (FSO)

DIM Testfolder

DIM Subfolder

DIM file

'There are two ways to delete files:

Fso.Deletefile (TestfilePath & "/beatles/octopusgarden.txt")

Set file = fso.getfile (TestfilePath & "/beatles/bathroomwindow.txt")

File.delete

'There are two ways to delete folders:

Fso.Deletefolder (TestfilePath & "/ beatles")

Fso.Deletefile (TestfilePath & "/ReadMe.txt")

Set testfolder = fso.getfolder (TestFilePath)

TestFolder.delete

End Sub

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''

'Createlyrics

'

'Purpose:

'

'Create two text files in the folder.

'

'

'Demonstration below

'

'- FileSystemObject.createTextFile

'- TextStream.writeline

'- TextStream.write

'- TextStream.writeblankLines

'- TextStream.Close

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

SUB CreatelyRics (Folder)

Dim TextStream

Set textStream = folder.createtextfile ("octopusgarden.txt")

TextStream.write ("Octopus 'Garden")' Please note that this statement does not add a wrap to the file.

TextStream.writeline ("(by ringo starr)")

TextStream.writeblankLines (1)

TextStream.writeline ("I'd Like to Be Under The Sea in An Octopus' Garden In The Shade,")

TextStream.writeline ("He'd Let Us in, Knows Where We've Been - In His Octopus' Garden in The Shade.")

TextStream.writeblanklines (2)

TextStream.close

Set textStream = folder.createtextfile ("bathroomwindow.txt")

TextStream.writeline ("She Came in THROUGH The bathroom window")

TextStream.writeline ("") TextStream.writeline ("She Came in Through a silver spoon")

TextStream.writeline ("But now She sucks her thumb and wanders by the banns of her own lagoon")

TextStream.writeblanklines (2)

TextStream.close

End Sub

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

'

'Getlyrics

'

'Purpose:

'

'Display the contents of the Lyrics file.

'

'

'Demonstration below

'

'- FileSystemObject.OpenTextFile

'- FileSystemObject.getfile

'- TextStream.readall

'- TextStream.Close

'- file.openastextStream

'- TextStream.atendofStream

'- TextStream.Readline

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

Function GetlyRics (FSO)

Dim TextStream

DIM S

DIM file

'There are a variety of ways to open a text file, and multiple methods to read data from the file.

'This uses two methods to open files and read files:

Set textStream = fso.opentextfile (TestFilePath & "/beatles/octopusgarden.txt", OpenFileforReading)

S = TEXTSTREAM.READALL & NEWLINE & Newline

TextStream.close

Set file = fso.getfile (testfilepath & "/beatles/bathroomwindow.txt")set textstream = file.openastextStream (OpenFileforReading)

Do While Not TextStream.atendofstream

S = S & TextStream.readline & newline

Loop

TextStream.close

Getlyrics = s

END FUNCTION

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

'

'BuildtestDirectory

'

'Purpose:

'

'Creating a directory hierarchy to demonstrate FileSystemObject.

'

'Create a hierarchical structure in this order:

'

'C: / test

'C: /test/readme.txt

'C: / Test / Beatles

'C: /test/beatles/octopusgarden.txt

'C: /test/beatles/bathroomwindow.txt

'

'

'Demonstration below

'

'- FileSystemObject.driveexists

'- FileSystemObject.foldeRexists

'- FileSystemObject.createFolder

'- FileSystemObject.createTextFile

'- folders.Add

'- Folder.createTextFile

'- TextStream.writeline

'- TextStream.Close

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

Function BuildtestDirectory (FSO)

DIM Testfolder

DIM Subfolders

DIM Subfolder

Dim TextStream

'Exclude (a) The drive does not exist, or (b) the case where the directory to be created already exists. IF not fso.driveexists (testdrive) THEN

BuildtestDirectory = false

EXIT FUNCTION

END IF

If fso.folderexists (testfilepath) THEN

BuildtestDirectory = false

EXIT FUNCTION

END IF

Set testfolder = fso.createfolder (testfilepath)

Set textStream = fso.createtextfile (TestFilePath & "/ReadMe.txt")

TextStream.writeline ("My Song Lyrics Collection")

TextStream.close

Set Subfolders = TestFolder.subfolders

Set Subfolder = Subfolders.Add ("Beatles")

CreatelyRics Subfolder

BuildtestDirectory = true

END FUNCTION

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

'

'Main program

'

'First, it creates a TEST directory and some subfolders and files.

'Then, it dumps some information about the available disk drives and Test directories.

'Finally, clear the Test directory and all its contents.

'

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

Sub main

DIM FSO

'Set up global variables.

TabStop = CHR (9)

NEWLINE = CHR (10)

SET FSO = CreateObject ("scripting.filesystemObject")

IF NOT BuildTestDirectory (fso) THEN

Print "Test Directory Already EXISTS OR Cannot Be created." EXIT SUB

END IF

Print GeneratedRiveInformation (FSO) & newline & newline

Print GenerateTestinformation (FSO) & newline & newline

Print GetlyRics (FSO) & newline & newline

DeletetestDirectory (FSO)

End Sub

I believe this example has covered most of the FSO applications, you can modify it according to this example, get yourself

Want's function, expectation and everyone exchange :)

CPPLUS

转载请注明原文地址:https://www.9cbs.com/read-4262.html

New Post(0)