Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (43)
  ADO (16)
  ADO.NET (11)
  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)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : .NET : ASP : Working with Drives, Folders and Files in ASP
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

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.

Previous ( 1 Gone )( 2 Remaining ) Next

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.

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