SoFunction
Updated on 2025-04-11

FileSystemObject Sample Code

In the sample code described in this section, real-life examples are provided to demonstrate many of the features available in the FileSystemObject object pattern. This code shows all the features of how to use object patterns together, and how to use them effectively in your own code.

Note that since the code is extremely general, it may take some additional code and minor changes to make it work on your machine. These changes are necessary because different methods are used for input and output for users between Active Server Pages and Windows Scripting Host.

To run the code on Active Server Pages, take the following steps:

Create a standard web page with the suffix name .asp.

Copy the following sample code into the file between the &ltBODY>...</BODY> tags.

Wrapping all code into the <%...%> tag.

Move the Option Explicit statement from the current location to the top of the HTML page, even before the <HTML> starts marking.

Place the <%...%> tag around the Option Explicit statement to ensure it runs on the server side.

Add the following code to the end of the example code:

Sub Print(x)
"<PRE>&ltFONT FACE=""Song style"" SIZE=""1"">"
 x
 "</FONT></PRE>"
End Sub
Main 
The previous code adds a printing process that will run on the server side but displays the results on the client side. To run the code on Windows Scripting Host, add the following code to the end of the sample code:
Sub Print(x)
 x
End Sub
Main
The following is the sample code:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' FileSystemObject Sample Code
'Copyright 1998 Microsoft Corporation. All rights reserved.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''OptionExplicit''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''
' For code quality:
' 1) The following code has many string operations, and the "&" operator is used to concatenate short strings together. because
' String concatenation is time-consuming, so this is an inefficient way to write code. Anyway, it is
' A very well maintained method of writing code, and this method is used here because the program executes
' There are a lot of disk operations, and disk operations are much slower than the memory operations required to connect strings.
' Remember this is demonstration code, not product code.
'
' 2) "Option Explicit" was used because accessing declared variables is more important than accessing undeclared variables
' Slightly faster. It can also prevent errors in the code, for example, mistakenly spelling DriveTypeCDROM
' Become DriveTypeCDORM.
'
' 3) In order to make the code more readable, there is no error handling in the code. Although preventive measures have been taken to ensure the code
' There is no error in normal circumstances, but the file system is unpredictable. In the product code, use
' On Error Resume Next and Err Objects to catch possible errors.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Some easily obtained global variables
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim TabStop
Dim NewLineConst TestDrive = "C"
Const TestFilePath = "C:\Test"'''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''
' The constant returned by
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const DriveTypeRemovable = 1
Const DriveTypeFixed = 2
Const DriveTypeNetwork = 3
Const DriveTypeCDROM = 4
Const DriveTypeRAMDisk = 5''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''
' The constant returned by
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const FileAttrNormal = 0
Const FileAttrReadOnly = 1
Const FileAttrHidden = 2
Const FileAttrSystem = 4
Const FileAttrVolume = 8
Const FileAttrDirectory = 16
Const FileAttrArchive = 32 
Const FileAttrAlias = 64
Const FileAttrCompressed = 128''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''
' Constant used to open a file
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const OpenFileForReading = 1 
Const OpenFileForWriting = 2 
Const OpenFileForAppending = 8 '''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''
' ShowDriveType
' Purpose:
' Generate a string to describe the drive type of a given Drive object.
' Demonstrate the following content
' - 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ShowDriveType(Drive) Dim S

Select Case 
Case DriveTypeRemovable
S = "Removable"
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 = SEnd Function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''

' ShowFileAttr
' Purpose:
' Generate a string to describe the properties of a file or folder.
' Demonstrate the following content
' - 
' - 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''Function ShowFileAttr(File) ' File can be a file or folder Dim S
Dim Attr

Attr =  If Attr = 0 Then
ShowFileAttr = "Normal"
Exit Function
End If If Attr And FileAttrDirectory Then S = S & "Directory "
If Attr And FileAttrReadOnly Then S = S & "Read-Only "
If Attr And FileAttrHidden Then S = S & "Hidden "
If Attr And FileAttrSystem Then S = S & "System "
If Attr And FileAttrVolume Then S = S & "Volume "
If Attr And FileAttrArchive Then S = S & "Archive "
If Attr And FileAttrAlias Then S = S & "Alias "
If Attr And FileAttrCompressed Then S = S & "Compressed " ShowFileAttr = SEnd Function''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''
' GenerateDriveInformation
' Purpose:
' Generate a string to describe the current state of the available drive.
' Demonstrate the following content
' -  
' - Iterating the Drives collection
' - 
' - 
' - 
' - 
' - 
' - 
' - 
' - 
' - 
' - 
' - 
' - 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''Function GenerateDriveInformation(FSO) Dim Drives
Dim Drive
Dim S Set Drives = 
S = “Number of drives:” & TabStop & NewLine & NewLine ' Construct 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 ' Construct the second line of the 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 ' Separated lines.
S = S & String(105, "-") & NewLine For Each Drive In Drives
S = S & 
S = S & TabStop & 
S = S & TabStop & ShowDriveType(Drive)
S = S & TabStop &  If  Then
If DriveTypeNetwork =  Then
S = S & TabStop &  
Else
S = S & TabStop &  
End If 
S = S & TabStop & 
S = S & TabStop & 
S = S & TabStop & 
S = S & TabStop & 
S = S & TabStop & Hex()
End If S = S & NewLine Next GenerateDriveInformation = SEnd Function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''

' GenerateFileInformation
' Purpose:
' Generate a string to describe the current state of the file.
' Demonstrate the following content
' - 
' - 
' - 
' - 
' - 
' - 
' - 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''Function GenerateFileInformation(File) Dim S S = NewLine & "Path:" & TabStop & 
S = S & NewLine & "Name:" & TabStop & 
S = S & NewLine & "Type:" & TabStop & 
S = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(File)
S = S & NewLine & "Created:" & TabStop & 
S = S & NewLine & "Accessed:" & TabStop & 
S = S & NewLine & "Modified:" & TabStop & 
S = S & NewLine & "Size" & TabStop &  & NewLine GenerateFileInformation = SEnd Function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GenerateFolderInformation
' Purpose:
' Generate a string to describe the current state of the folder.
' Demonstrate the following content
' - 
' - 
' - 
' - 
' - 
' - 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Function GenerateFolderInformation(Folder) Dim S S = "Path:" & TabStop & 
S = S & NewLine & "Name:" & TabStop & 
S = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(Folder)
S = S & NewLine & "Created:" & TabStop & 
S = S & NewLine & "Accessed:" & TabStop & 
S = S & NewLine & "Modified:" & TabStop & 
S = S & NewLine & "Size:" & TabStop &  & NewLine GenerateFolderInformation = SEnd Function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GenerateAllFolderInformation
' Purpose:
'  Generate a string to describe the current status of a folder and all files and subfolders.
' Demonstrate the following content
' - 
' - 
' - 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Function GenerateAllFolderInformation(Folder) Dim S
Dim SubFolders
Dim SubFolder
Dim Files
Dim File S = "Folder:" & TabStop &  & NewLine & NewLine
Set Files =  If 1 =  Then
S = S & "There is 1 file" & NewLine
Else
S = S & "There are " &  & " files" & NewLine
End If If  <> 0 Then
For Each File In Files
S = S & GenerateFileInformation(File)
Next
End If Set SubFolders =  If 1 =  Then
S = S & NewLine & "There is 1 sub folder" & NewLine & NewLine
Else
S = S & NewLine & "There are " &  & " sub folders" & NewLine & NewLine
End If If  <> 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 = SEnd Function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''
' GenerateTestInformation
' Purpose:
' Generate a string to describe the current status of the C:\Test folder and all files and subfolders.
' Demonstrate the following content
' - 
' - 
' - 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''Function GenerateTestInformation(FSO) Dim TestFolder
Dim S If Not (TestDrive) Then Exit Function
If Not (TestFilePath) Then Exit Function Set TestFolder = (TestFilePath) GenerateTestInformation = GenerateAllFolderInformation(TestFolder) End Function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''
' DeleteTestDirectory
' Purpose:
' Clean the test directory.
' Demonstrate the following content
' - 
' - 
' - 
' - 
' - 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''Sub DeleteTestDirectory(FSO) Dim TestFolder
Dim SubFolder
Dim File

' There are two ways to delete files: (TestFilePath & "\Beatles\") Set File = (TestFilePath & "\Beatles\")
'There are two ways to delete a folder:
(TestFilePath & "\Beatles")
(TestFilePath & "\")
Set TestFolder = (TestFilePath)
 Sub'''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''
' CreateLyrics
' Purpose:
' Create two text files in the folder.
' Demonstrate the following content
' - 
' - 
' - 
' - 
' - 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''Sub CreateLyrics(Folder) Dim TextStream

Set TextStream = ("")

("Octopus' Garden ") ' Please note that this statement does not add line wraps to the file.
("(by Ringo Starr)")
(1)
("I'd like to be under the sea in an octopus' garden in the shade,")
("He'd let us in, knows where we've been -- in his octopus' garden in the shade.")
(2)

 Set TextStream = ("")
("She Came In Through The Bathroom Window (by Lennon/McCartney)")
("")
("She came in through the bathroom window protected by a silver spoon")
("But now she sucks her thumb and wanders by the banks of her own lagoon")
(2)
 Sub''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GetLyrics
' Purpose:
' Show the contents of the lyrics file.
' Demonstrate the following content
' - 
' - 
' - 
' - 
' - 
' - 
' - 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''Function GetLyrics(FSO) Dim TextStream
Dim S
Dim File ' There are multiple ways to open a text file, and multiple ways to read data from a file.
' There are two methods to open and read files here: Set TextStream = (TestFilePath & "\Beatles\", OpenFileForReading)

S =  & NewLine & NewLine
 Set File = (TestFilePath & "\Beatles\")
Set TextStream = (OpenFileForReading)
Do While Not 
S = S &  & NewLine
Loop
 GetLyrics = S

End Function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''
' BuildTestDirectory
' Purpose:
' Create a directory hierarchy to demonstrate FileSystemObject.
' Create a hierarchical structure in this order:
' C:\Test
' C:\Test\
' C:\Test\Beatles
' C:\Test\Beatles\
' C:\Test\Beatles\
' Demonstrate the following content
' - 
' - 
' - 
' - 
' - 
' - 
' - 
' - 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''Function BuildTestDirectory(FSO) Dim TestFolder
Dim SubFolders
Dim SubFolder
Dim TextStream
' Exclude (a) the drive does not exist, or (b) the directory to be created already exists. If Not (TestDrive) Then
BuildTestDirectory = False
Exit Function
End If If (TestFilePath) Then
BuildTestDirectory = False
Exit Function
End If Set TestFolder = (TestFilePath) Set TextStream = (TestFilePath & "\")
("My song lyrics collection")
 Set SubFolders = 
Set SubFolder = ("Beatles")
CreateLyrics SubFolder 
BuildTestDirectory = TrueEnd Function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''
' Main program
' First, it creates a test directory, as well as some subfolders and files.
' It then 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 global variables.
TabStop = Chr(9)
NewLine = Chr(10)

Set FSO = CreateObject("") If Not BuildTestDirectory(FSO) Then 
Print "Test directory already exists or cannot be created. Cannot continue."
Exit Sub
End If Print GenerateDriveInformation(FSO) & NewLine & NewLine
Print GenerateTestInformation(FSO) & NewLine & NewLine
Print GetLyrics(FSO) & NewLine & NewLine
DeleteTestDirectory(FSO)End Sub