ASP Folder Object

The folder object represents a specified file folder on the current machine. We can use the properties and methods of the Folder object to traverse directories on the drive, and to get at all the properties of this or any other folder. To work with the Folder object’s properties and methods, you need to create an instance of the FileSystemObject object first and then instantiate the Folder object through the GetFolder method. Also you can get a Folder object reference from the Drive object, by using the RootFolder property.

The collections, properties and methods of the Folder object are as follows:

Collections

Collections Description
Files The Files collection returns the collection of all the files in the specified folder. It does not represent files existing in subfolders of the current folder.
SubFolders The SubFolders collection returns the collection of all subfolders in the specified folder, including hidden and system folders.

Properties

Properties Description
Attributes The Attributes property sets or returns (depending on the specific folder attribute) the attributes of the specified folder.
DateCreated The DateCreated property returns the date and time that the specified folder was created.
DateLastAccessed The DateLastAccessed property returns the date and time that the specified folder was last accessed.
DateLastModified The DateLastModified property returns the date and time that the specified folder was last modified.
Drive The Drive property returns the drive letter of the drive on which the specified folder resides.
IsRootFolder The IsRootFolder property returns true if the folder is the root folder of the current drive and false if not.
Name The Name property sets or returns the name of the specified folder.
ParentFolder The ParentFolder returns the Folder object for the parent folder of the specified folder.
Path The Path property returns the absolute path of the specified folder.
ShortName The ShortName property returns the DOS-style version of the folder name (the 8.3 naming convention).
ShortPath The ShortPath property returns the DOS-style version of the absolute path of the specified folder (the 8.3 naming convention).
Size The Size property returns the size of all files and subfolders contained in the specified folder (in bytes).
Type The Type property returns the type of the specified folder (such as “Recycle Bin”) if available.

Methods

Methods Description
Copy The Copy method copies the specified folder from one folder to another.
Delete The Delete method deletes the specified folder.
Move The Move method moves the specified folder from one folder to another.
CreateTextFile Creates a new text file in the specified folder and returns a TextStream object that refers to it.

How to use the ASP Folder Object

In the following example we create an instance of the FileSystemObject object and then use the GetFolder method to instantiate the Folder object. After that we use The Folder object properties to get the information about the specified folder:

<%
Dim objFSO, objDrive
'Create a FileSystemObject instance
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Using the GetDrive method of FileSystemObject object, initialize a Drive object
Set objFolder = objFSO.GetFolder("c:\test")
'Retrieve information about the folder
Response.Write "Name: " & objFolder.Name & "<br />"
Response.Write "ShortName: " & objFolder.ShortName & "<br />"
Response.Write "Size: " & objFolder.Size & " bytes <br />"
Response.Write "Type: " & objFolder.Type & "<br />"
Response.Write "Path: " & objFolder.Path & "<br />"
Response.Write "ShortPath: " & objFolder.ShortPath & "<br />"
Response.Write "Created: " & objFolder.DateCreated & "<br />"
Response.Write "LastModified: " & objFolder.DateLastModified & "<br /><br />"
%>

The example below iterates through all the folders in the root of drive C:, and displays the date and time that each one was created:

<%
Dim objFSO, objDrive
'Create a FileSystemObject instance
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Using the GetDrive method of FileSystemObject object, initialize a Drive object
Set objDrive = objFSO.GetDrive("c:")
'Get a reference to the root folder
Set objRoot = objDrive.RootFolder
'Get a reference to the folders collection
Set colFolders = objRoot.SubFolders
'Retrieve information about all the folders in within this folder
For Each objFolder in colFolders
Response.Write "Name: " & objFolder.Name & "<br />"
Response.Write "LastModified: " & objFolder.DateLastModified & "<br /><br />"
Next
%>
admin

admin

Leave a Reply

Your email address will not be published.