Getting the list of files inside a directory by using FileSystemObject in ASP

By using FileSystemObject (FSO)we can list out all the files of a directory. Here we will be using server MapPath to map the virtual path to real path as used by file system object. After initiating the object we can instantiate the folder object. We will be using this folder object Files property to get all the files present within the folder.


Before this it is advisable to learn the FileSystemObject, Server MapPath to convert virtual path to real path before reading this.


In our example we have used the folder name my_folder which is inside the directory name t so the path of the folder is

https://mysite.com/t/my_folder/

Here is the code.

Dim objFSO, objFile, objFolder

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("/t/my_folder"))

For Each objFile in objFolder.Files
Response.Write objFile.Name & "<br>"
Next
Set objFolder = Nothing
Set objFSO = Nothing

Displaying last updated date along with the file name

We can add the last modified date of the file by adding the DateLastModified property of the File object. This one line we will add inside the For Each Loop.

For Each objFile in objFolder.Files
Response.Write objFile.Name & " : "
Response.Write objFile.DateLastModified & "<br>"
Next

Be the first to post comment on this article :




Google+

Post Comment This is for short comments only. Use the forum for more discussions.




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer