Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP : Working with Drives, Folders and Files in ASP
 

Working with Drives, Folders and Files in ASP

by Faisal Khan.Follow Faisal Khan on Twitter

Overview
This article is first of a series of articles about working with drives, folders and files using ASP. In this article we'll learn how to get hold of Drives collection which FileSystemObject provides us, then we'll use this handle to display a list of all drives connected to our computer with their detailed info. These drives may be fixed hard disks, removable drives like floppy drives, CD-ROM drives or even network drives. We'll then use the handle to a drive to access root folder for that drive and then display all the sub folders and files in that drive. We'll build our application in such a way that we can move around different drives and folders and read different files.

To sum up you'll learn how to get hold of drives, display list of folders and subfolders, get hold of a folder and display list of files it contains along with their detailed info, and also how to read a file and display it's contents using ASP.

Why would I need to work with folders and files?
With any web application you build sooner or later you come across the need to read and write files to the server hard disk. ASP makes it easy for you. ASP allows you to do just about anything you can think of with files using FileSystemObject that comes with IIS.

What are the security considerations?
Well any application on your web site which allows you to read and write files to your server hard disk can be potential security threat if someone else gets access to that ASP page. So as a rule of thumb you should always password protect such applications and don't make them public.

What will I need?
Nothing, IIS comes preloaded with FileSystemObject. You'll have no problem in using it on your own computer or your site server. Remember though that some virtual ASP hosting account providers don't allow you to use FileSystemObject on their servers, so before using it on your server always check and see if your ASP hosting provider supports that. Almost all good ASP hosts I know off support FileSystemObject.

Following is a list of topics we'll cover in this article :

Working with Drives
We will build an ASP page drive.asp that will display a list of all drives on your system. As explained on the previous page we'll be using FileSystemObject.

There is no need to manually create drive.asp page. At the end of this article you can download all the source code.

Lets first create FileSystemObject.

<%
Dim fso
	Set fso = Server.CreateObject("Scripting.FileSystemObject")
	

There is property of FileSystemObject called Drives which returns a collection of drive objects available on the local machine. We get hold of that property by using the ASP Set keyword. Note that whenever you want to reference an object or collection always use Set keyword.

Dim drives
	Set drives = fso.Drives
	

Now drives variable points to the Drives Collection. Notice that Drives Collection is not a single Drive object, it rather is a Collection of Drive objects.

To reference each element from a collection we use standard ASP For...Each loop.

Dim isReady

For Each drive in drives
	isReady = drive.IsReady

	If isReady Then
		Response.Write "DriveLetter: " & drive.DriveLetter & "<br>"
		Response.Write "Path: " & drive.Path & "<br>"
		Response.Write "FileSytem: " & drive.FileSystem & "<br>"
		Response.Write "TotalSize: " & drive.TotalSize & "<br>"
		Response.Write "FreeSpace: " & drive.FreeSpace & "<br>"
	Else
		Response.Write "Driv Letter: " & drive.DriveLetter & "<br>"
		Response.Write drive.Path & "<br>"
		Response.Write "No other information available."
	End If
Next

In the code above we first declare a variable isReady and then enter the For..Each loop. This loop iterates through all the elements of Drives collection. Within the loop we get hold of individual Drive object and use Response.Write to display Drive object's different properties.

You'll be wondering what IsReady property of Drive object means. Well this property returns a boolean value ( True or False ) indicating if the drive is ready or not. What do we mean by a drive being ready or not ? A drive is ready when it is connected to the system and working e.g. our fixed hard disks. A drive is not ready e.g. when there is no CD in CD-ROM drive or there is no floppy in the floppy drive.

When we are done with FileSystemObject and Drive objects we explicitly remove them from server memory using the Nothing keyword.

	Set drives = Nothing
	Set fso = Nothing
%>
	

Note that drive.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 Folders
Here we will build folder.asp page to display a list of all subfolders and files within a given folder. Like before there is no need to copy/paste the code, simply try to understand what is going on, all the source code is available for download on the last page.

We begin by creating the FileSystemObject.

<%
Dim fso
	Set fso = Server.CreateObject("Scripting.FileSystemObject")

Lets get reference to the root folder for our website using FileSystemObject's GetFolder method.

Dim rootFolder
	Set rootFolder = fso.GetFolder(Server.MapPath("/"))

Now we use the handle to the root folder of our web site to get a collection of sub folders using Folder object's SubFolders property.

Dim subFolders
	Set subFolders = rootFolder.SubFolders

We are now ready to use For..Each loop to display a list of all sub folders under the root folder.

For Each folder in subFolders
	Response.Write "FolderName: " & folder.Name & "<br>"
	Response.Write "Attributes: " & folder.Attributes & "<br>"
	Response.Write "Type: " & folder.Type & "<br>"
	Response.Write "Size: " & folder.Size & "<br>"
	Response.Write "ShortName: " & folder.ShortName & "<br>"
Next

Overview
This article is first of a series of articles about working with drives, folders and files using ASP. In this article we'll learn how to get hold of Drives collection which FileSystemObject provides us, then we'll use this handle to display a list of all drives connected to our computer with their detailed info. These drives may be fixed hard disks, removable drives like floppy drives, CD-ROM drives or even network drives. We'll then use the handle to a drive to access root folder for that drive and then display all the sub folders and files in that drive. We'll build our application in such a way that we can move around different drives and folders and read different files.

To sum up you'll learn how to get hold of drives, display list of folders and subfolders, get hold of a folder and display list of files it contains along with their detailed info, and also how to read a file and display it's contents using ASP.

Why would I need to work with folders and files?
With any web application you build sooner or later you come across the need to read and write files to the server hard disk. ASP makes it easy for you. ASP allows you to do just about anything you can think of with files using FileSystemObject that comes with IIS.

What are the security considerations?
Well any application on your web site which allows you to read and write files to your server hard disk can be potential security threat if someone else gets access to that ASP page. So as a rule of thumb you should always password protect such applications and don't make them public.

What will I need?
Nothing, IIS comes preloaded with FileSystemObject. You'll have no problem in using it on your own computer or your site server. Remember though that some virtual ASP hosting account providers don't allow you to use FileSystemObject on their servers, so before using it on your server always check and see if your ASP hosting provider supports that. Almost all good ASP hosts I know off support FileSystemObject.

Following is a list of topics we'll cover in this article :

Working with Drives
We will build an ASP page drive.asp that will display a list of all drives on your system. As explained on the previous page we'll be using FileSystemObject.

There is no need to manually create drive.asp page. At the end of this article you can download all the source code.

Lets first create FileSystemObject.

<%
Dim fso
	Set fso = Server.CreateObject("Scripting.FileSystemObject")
	

There is property of FileSystemObject called Drives which returns a collection of drive objects available on the local machine. We get hold of that property by using the ASP Set keyword. Note that whenever you want to reference an object or collection always use Set keyword.

Dim drives
	Set drives = fso.Drives
	

Now drives variable points to the Drives Collection. Notice that Drives Collection is not a single Drive object, it rather is a Collection of Drive objects.

To reference each element from a collection we use standard ASP For...Each loop.

Dim isReady

For Each drive in drives
	isReady = drive.IsReady

	If isReady Then
		Response.Write "DriveLetter: " & drive.DriveLetter & "<br>"
		Response.Write "Path: " & drive.Path & "<br>"
		Response.Write "FileSytem: " & drive.FileSystem & "<br>"
		Response.Write "TotalSize: " & drive.TotalSize & "<br>"
		Response.Write "FreeSpace: " & drive.FreeSpace & "<br>"
	Else
		Response.Write "Driv Letter: " & drive.DriveLetter & "<br>"
		Response.Write drive.Path & "<br>"
		Response.Write "No other information available."
	End If
Next

In the code above we first declare a variable isReady and then enter the For..Each loop. This loop iterates through all the elements of Drives collection. Within the loop we get hold of individual Drive object and use Response.Write to display Drive object's different properties.

You'll be wondering what IsReady property of Drive object means. Well this property returns a boolean value ( True or False ) indicating if the drive is ready or not. What do we mean by a drive being ready or not ? A drive is ready when it is connected to the system and working e.g. our fixed hard disks. A drive is not ready e.g. when there is no CD in CD-ROM drive or there is no floppy in the floppy drive.

When we are done with FileSystemObject and Drive objects we explicitly remove them from server memory using the Nothing keyword.

	Set drives = Nothing
	Set fso = Nothing
%>
	

Note that drive.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 Folders
Here we will build folder.asp page to display a list of all subfolders and files within a given folder. Like before there is no need to copy/paste the code, simply try to understand what is going on, all the source code is available for download on the last page.

We begin by creating the FileSystemObject.

<%
Dim fso
	Set fso = Server.CreateObject("Scripting.FileSystemObject")

Lets get reference to the root folder for our website using FileSystemObject's GetFolder method.

Dim rootFolder
	Set rootFolder = fso.GetFolder(Server.MapPath("/"))

Now we use the handle to the root folder of our web site to get a collection of sub folders using Folder object's SubFolders property.

Dim subFolders
	Set subFolders = rootFolder.SubFolders

We are now ready to use For..Each loop to display a list of all sub folders under the root folder.

For Each folder in subFolders
	Response.Write "FolderName: " & folder.Name & "<br>"
	Response.Write "Attributes: " & folder.Attributes & "<br>"
	Response.Write "Type: " & folder.Type & "<br>"
	Response.Write "Size: " & folder.Size & "<br>"
	Response.Write "ShortName: " & folder.ShortName & "<br>"
Next

 ( 3 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Comments/Questions ( Threads: 26, Comments: 39 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Copy images in Access on to the hard disk
  2. plz help me
  3. Determined folder of the drive D
  4. Urgent!! problem with listing files ( 1 Reply )
  5. Trouble Accessing DLL files Thru ASP
  6. Need assistance with directory creation please.... ( 1 Reply )
  7. Permission to Folder
  8. Not able to read contents from file.
  9. How to read/write a textfile; coding in Windows XP/IIS
  10. Web Drive
  11. Network Drives
  12. Want to display files in a folder sorted by date created? ( 2 Replies )
  13. Listing Folders in Dropdown menu ( 1 Reply )
  14. Is Great!
  15. display folders within folders
  16. it doesnt work?
  17. Permission Denied on root of C drive
  18. This is great stuff ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  19. Attach a file to an Email From Database
  20. adding/creating a new folder ( 2 Replies )
  21. Disk is not ready??
  22. error with getfolder method ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  23. Errors using IIS in Windows 2000 Professional ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  24. filesysytem objects ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  25. How to read a delimeted(delimeter ||) text file ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  26. Exactly what I was looking for ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.