views:

31

answers:

1

I am wanting to databind a DropDownList control with a list of all sub directories that exist in a particular directory on the server. The directory I want to search is in the root of the application. I am fairly new to programming and I'm not sure where to even start.

I found this code on a website:

 Dim root As String = "C;\"
        Dim folders() As String = Directory.GetDirectories(root)  
        Dim sb As New StringBuilder(2048)  
        Dim f As String  
        For Each f In folders  
            Dim foldername As String = Path.GetFileName(f)  
            sb.Append("<option>")  
            sb.Append(foldername)  
            sb.Append("</option>")  
        Next  
        Label3.Text = "<select runat=""sever"" id=""folderlist""" & sb.ToString() & "</select>"  

I guess this is vb. but my tool is in asp, so is their something similar in vbscript so that I can use it.

A: 

Here is a very quick and dirty example of what you want.

There are a lot of improvements I would make before I'd consider it production ready code.

It should however server to show you some of the basic concepts you are after.

<%@ Language=VBScript  ENABLESESSIONSTATE = False%>

<select id="selFiles" name="selFiles" class="Select" style="width: 250px" tabindex="130">

<% 

Dim fso, folder, files

Set fso = CreateObject("Scripting.FileSystemObject")  

Set folder = fso.GetFolder("C:\")  

Set files = folder.Files    
For each folderIdx In files 

    Response.Write("<option>" + folderIdx.Name + "</option>")

Next  

 %>
</select>

One place to start looking at improvements here would be introducing your own components that will do all the complex stuff like listing files this gives you more control, allows greater modularity in you design and (probably most importantly) gives you better control of security.

The information below may be slightly off (it is from memory of an old project) but should be reasonably close and give you a start on introducing code components into your ASP classic code.

With ASP classic you create objects using code like:

<object runat="server" progid="YourObject.Class" id="oObject" VIEWASTEXT></object>

Where YourObject.Class is the programatic id of a component installed in the registry.

David Hall
this is way too technical to me, coz i am just a beginner. but i will try and understand it. thanks a lot for the help.
sushant
@sushant If you are just working on your own projects then don't worry too much, learn at your pace, and focus on the solution I gave and try to understand the FileSystemObject and how to create html code on the fly. If you are being paid to do this work then I would ask your employer to arrange you some training, since it is their business that the security risks will affect and their responsibility to give you the tools you need.
David Hall
thanks for the concern. m doing my own project so i'll probably learn by myself. by the way what security risk are we talking about?
sushant
@sushant I don't have any concrete security risk in mind. All the vbscript code should be run on your server and be free from tampering. The main risk would be needing to allow the process that executes your website any elevated rights to your file system. Listing files in your application directory should be fine. That said, my ASP classic is very rusty and I'd hate to open you up to a security hold so though I'd mention the way I've done similar things in the past. Have fun learning!
David Hall
Use Server.MapPath("/") to get the directory of your website/application.Also you may want to make this routine recursive if you also need multiple levels of sub-directories.
thomask
sushant
@sushant there are really three things at play here - one is scripting asp (how you use the <% %> to inject code. One is learnign more about vbscript as a language. Finally there is the FileSystemObject which is a topic in itself. Googling about each of these should server you to start learning.
David Hall