Above loop does pretty much the same job as it did on the previous page. It iterates
through the collection of sub folders and we simple display different properties of the Folder
object.
Now the next step is to use a similar For..Each loop and display a list
of files under the root folder.
Dim files
Set files = rootFolder.Files
We first get hold of Files collection by using our root folder's File
property which returns a collection of File objects under that folder.
For Each file in files
Response.Write "FileName: " & file.Name & "<br>"
Response.Write "Attributes: " & file.Attributes & "<br>"
Response.Write "Type: " & file.Type & "<br>"
Response.Write "Size: " & file.Size & "<br>"
Response.Write "ShortName: " & file.ShortName & "<br>"
Next
Next we simple iterate through the collection using For..Each loop and display
different poperties of File object.
We are done so we set all the object references to Nothing.
Set files = Nothing
Set subFolders = Nothing
Set rootFolder = Nothing
Set fso = Nothing
Note that folder.asp page present in the download contains the same code plus
some extra code to display the same results using more convenient to read HTML coding.
Working with Files
Now we will build file.asp page which reads content of a file and displays
them on the user screen.
We first declare a variable fileName which receives complete path to
the file we want to open for reading.
<%
Dim fileName
fileName = Trim(Request.QueryString("file"))
%>
Lets create FileSystemObject to begin with.
<%
Dim fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")
OpenTextFile method of FileSystemObject returns a
TextStream object. We then use different methods of TextStream
object to read contents of the file.
Dim stream
Set stream = fso.OpenTextFile(fileName)
Then using a Do..While loop we read through the content of the file line
by line and display it to the user screen. During the loop we check to see if we have
reached the end of file by using the AtEndOfStream property of TextStream
object.
Dim lineText
Do While Not stream.AtEndOfStream
lineText = stream.ReadLine
lineText = Replace (lineText,"<","<")
lineText = Replace (lineText,">",">")
Response.Write lineText & vbcrlf
Loop
Once we are done with the file, we close it by using TextStream.Close
method.
stream.Close
We are done so we set all the object references to Nothing.
Set stream = Nothing
Set fso = Nothing
Note that file.asp page present in the download contains the same code plus
some extra code to display the same results using more convenient to read HTML coding.
Summary
In this article we learned how to get hold of Drives collection and
what are different properties of Drive object. Then by using FileSystemObject.GetFolder
method we got hold of a folder and then using SubFolders and Files
collection we displayed a list of sub folders and files in that folder. Next we learned
how to read through a file and display it's content to the user.
In all the examples we used FileSystemObject to work with drives, folders
and files. All it takes is just a few lines of code and not more. In this article I have
deliberately left some detailed issues of working with folders and files like deleting and
renaming folders, deleting, renaming, creating and writing to files etc. I'll cover them
in the second article of this series.
I have put up complete listing of different properties and methods of Drive,
Folder, File and TextStream object. So you can see
what other properties and methods are available for your use than the ones discussed in
this article.
Properties and Methods of FileSystemObject Object
Only property of FileSystemObject.
| Property |
Description |
Drives |
Returns a collection of Drive objects. |
Methods for working with drives.
| Method |
Description |
DriveExists(drive) |
Returns True if drive exists else reutrns False. |
GetDrive(drive) |
Returns a reference to the specified Drive object. |
GetDriveName(drive) |
Returns name of the drive specified. |
Methods for working with folders.
| Method |
Description |
BuildPath(path, name) |
Adds specified file or folder ( name ) to the existing path. |
CopyFolder(source, destination, overwrite) |
Copies all files and folders specified in the source to the destination.
Setting overwrite to True makes sure that destination files are
copied over with source file. |
CreateFolder(pathandname) |
Creates a new folder with path and name as specified in the pathandname
argument. |
DeleteFolder(name, force) |
Deletes the folder specified in the name argument including all sub
folders and files. If force is set to True then folders and files
with read-only attribute are also deleted. |
FolderExists(name) |
Returns True if folder with specified path and name exists. |
GetAbsolutePathName(path) |
Returns complete path using the path attribute. |
GetFolder(name) |
Returns reference to the Folder object specified in the name
attribute. |
GetParentFolderName(name) |
Returns name of the parent folder of the folder specified. |
GetSpecialFolder(name) |
Returns a Folder object corresponding to the special windows folder
specified. Argument may be WindowsFolder(0), SystemFolder(1) and
TemporaryFolder(2). |
MoveFolder(source, destination) |
Moves a folder from source to destination path. |