Sunteți pe pagina 1din 7

Working with Drives, Folders and Files in ASP

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 Response.Write Response.Write Response.Write Response.Write Else Response.Write Response.Write Response.Write End If "DriveLetter: " & drive.DriveLetter & "<br>" "Path: " & drive.Path & "<br>" "FileSytem: " & drive.FileSystem & "<br>" "TotalSize: " & drive.TotalSize & "<br>" "FreeSpace: " & drive.FreeSpace & "<br>" "Driv Letter: " & drive.DriveLetter & "<br>" drive.Path & "<br>" "No other information available."

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 Uploading Files to the Server Hard Disk using plain ASP Overview This article is number three in a series of articles I wrote about uploading and displaying binary data with ASP. The first article was about Uploading binary data to the database, second was on Displaying binary data from the database and the third one ( this one ) is going to be on 'Uploading files to the server hard disk using pure ASP'. File Uploading with ASP.NET If you have the privilege of using ASP.NET then you should read these comprehensive tutorials regarding file uploading using built-in ASP.NET server controls: 1. File uploading to server hard disk. 2. File uploading to Microsoft Access database. 3. Uploading images, determining size, width & height and resizing image files. I'll assume that you have read the previous articles and thus will only concentrate on the parts which are new in this article. I get enormous amounts of emails by people asking me how to upload files with ASP. This article is going to be an answer to all those questions. In all the previous articles we have been using Loader.asp to handle binary data. Same is going to be the case in this article. We'll though modify Loader.asp a bit to allow us to save uploaded binary data to the server hard disk. Loader.asp Open your note pad ( or any other ASP editor you use ) and create a new file. Save it as 'Loader.asp'. Now copy the text listed below and paste it in to the newly created 'Loader.asp' page and hit the save button :

Reading XML file with ASP Uses of XML XML stands for Extensible Markup Language. XML can be used in many ways and one of which is 'data storage'. This is the one we will be exploring in this article. XML

along with XSL ( Extensible Stylesheet Language ) can by used to present data on the web pages. XML provides the data and XSL allows us to present it the way we want. Remember though that not all browsers support XML on the client side, only Micrsoft Internet Explorer 5.0 and above support XML. XML can also be used to perform RPC ( Remote Procedure Call ). Actually this capability of XML to allow communication between distant applications is so strong that Microsoft has developed SOAP ( Simple Object Access Protocol ) specification which uses XML to allow communication between remote applications. XML can be used for a lot more purposes which I haven't mentioned here. Also keep in mind that Microsoft's future .NET platform will make use of XML even more than any tool does today. ASP+, ADO+ and others will use XML to define and present data. So if you are comfortable with XML today, it will help you in the near future when you will be getting yourself ready to develop applications in the revolutionary platform called Microsoft .NET. What are XML files ? If you know HTML then you already know 70% XML. XML is tag based just like HTML. The major difference between HTML and XML is that in XML you can define your own tags while in HTML you make use of pre-defined tags. XML files most often than not begin with following tag on top of the page : <?xml version="1.0"?> This line tells the XML parser the version of XML we are using. You for now don't need to change it, just remember to add it on top of every XML page you create. Every starting XML tag should have a corresponding end tag. <name>Faisal Khan</name> If you don't want to write end tag then simply add forward slash in the tag like this : <br/> Thus <br> tag of HTML will be written as <br/> in XML. Elements The tags in XML are known as 'elements'. 'elements' may or may not contain any content, thus following are all correct : <name>Faisal Khan</name> <name></name> <name/> Attributes Attributes are the same name / value pairs that you use in HTML. Elements may or may not contain attributes. Following is an example of an element containing an attribute : <name language="US-EN">Faisal Khan</name> This was a very simple and basic introduction to XML, on the next page we will see what is the difference between well formed and valid XML documents. Creating 'Page.xml' file We will now create a simple XML file; 'page.xml'. Open notepad and create a new file 'page.xml' and then copy paste the following code into it : <?xml version="1.0"?> <main> <title>Our Page.xml file</title> <heading>This is a test heading</heading>

<paragraph>This is our paragraph and you can write whatever you want to in this space.</paragraph> <testHTML><![CDATA[We will enclose some HTML code in CDATA section now :<br> <table width="60%" border="1" bordercolor="silver" cellspacing="2" cellpadding="3"> <tr> <td> You can write any HTML code or for that matter any type of text inside the CDATA section without a fear of getting any error.<br><br> Note if we write this without the CDATA tags, the xml parser will raise an error and won't show the document. </td> </tr> </table>]]></testHTML> </main> There are certain points to be noted in the code above. Notice that after writing the xml version line we have enclosed our three elements (title, heading, paragraph, testHTML) inside a single tag (main). This is so because in XML after the processing instructions (xml version tag in the beginning) all the elements and sub-elements that we define have to be enclosed within a single element whatever you name it. Thus in our case we defined that single element and named it 'main' which encloses our other 4 elements. Notice that in the 'testHTML' tag we have put lot of our normal HTML code. XML parser will raise an error when it encounters HTML code which doesn't abide to XML rules. To get through this we make use of <![CDATA[....]]> markup to tell the XML parser that the following text is character data and should not be parsed and evaluated like other XML data is done. CDATA actually stands for 'character data' and allows any kind of characters / text to be written inside it. So remember that whenever you want to enclose HTML inside your XML elements, enclose it between the CDATA markup tags. Rest of the code is easier to understand. In the first line we defined the processing instruction by telling the XML version to the XML parser. Then we created a 'main' tag element to enclose rest of our XML document. In the 'main' element we created 'title', 'heading', 'paragraph' and 'testHTML' tags to contain some text which will be read by our ASP page. Creating 'showxml.asp' page We will now create a very simple ASP page and code it to show the XML data that we created in the 'page.xml' file. Open notepad and create a new ASP page. Then copy paste the following code into it and save it : <% Option Explicit Response.Buffer = True Dim xml Set xml = Server.CreateObject("Microsoft.XMLDOM") xml.async = False xml.load (Server.MapPath("page.xml")) Dim title, heading, paragraph, testHTML

title = xml.documentElement.childNodes(0).text heading = xml.documentElement.childNodes(1).text paragraph = xml.documentElement.childNodes(2).text testHTML = xml.documentElement.childNodes(3).text Set xml = Nothing %> <html> <head> <title><%= title %></title> </head> <body> <h3 align="center"><%= heading %></h3> <p align="center"><% = paragraph %></p> <div align="center"><%= testHTML %></div> </body> </html> Put 'showxml.asp' page in the same directory where you have kept the 'page.xml' file. Explanation : Dim xml Set xml = Server.CreateObject("Microsoft.XMLDOM") We create the XML Document Object in the 'xml' variable. xml.async = False By setting '.async' option to False, we are saying to the XML parser to show the data as soon as it begins to read it. Note that by setting '.async' to False, retrieving of XML data is speeded up. xml.load (Server.MapPath("page.xml") Next we load the 'page.xml' file. The 'load()' method asks for complete physical path to the XML file. We give it the complete physical path by using 'Server.MapPath' method to convert the relative path to complete physical path. Dim title, heading, paragraph, testHTML title = xml.documentElement.childNodes(0).text heading = xml.documentElement.childNodes(1).text paragraph = xml.documentElement.childNodes(2).text testHTML = xml.documentElement.childNodes(3).text Basic Active Server Pages Tutorial for beginners Overview ASP stands for Active Server Pages. ASP is a server side technology which is used to display dynamic content on the web pages. ASP is becoming popular day by day as the favorite server side technology. ASP in itself isn't a language actually, instead it uses VBScript or JScript to display dynamic content. ASP is more of a technology used by VBScript / JScript on the server side. So if you know VBScript or JScript you can start creating .ASP pages right now. I will assume here that you are not familiar with VBScript, the language of choice for creating ASP pages. So I will also give you a little insight into VBScript too. But a general understanding of HTML is required. You must be wondering what is required to run an ASP page ? well if you are running windows then you can download Personal Web Server ( for Win95 & Win98 )

or Internet Information Server ( for WinNT ) from www.microsoft.com. If you are running Windows 2000 then you have got IIS 5 already, you just need to install it. Just go to the 'control panel' and click the 'Add/Remove Programs' icon. Then on the left click the 'Add/Remove Windows Components' button, a window will appear offering you a lot of choices. Just check where 'Internet Information Services' is written and click 'Next', your IIS 5 will be installed. For those of you who are running other operating systems please take a look at www.chilisoft.com. They offer ASP solution for those of us not running Microsoft Windows. Please note that Chilisoft products are not free but you can download PWS or IIS free of cost from Microsoft's web site above. In the next few pages I am going to teach you the basics of VBScript, ASP intrinsic objects, how to call ASP intrinsic objects and make use of them and what makes a .asp page different from a .htm / .html page. After the completion of this tutorial you will be able to start writing your own .asp pages. Following is a list of topics we'll cover in this article :

S-ar putea să vă placă și