ASP , Reading directories
ASP , Reading directories
Categories: Coding Tips, Featured Articles |
Well… I almost don’t work with web apps written in ASP.
However, every now and then a client asks for a site in this language.
And every single time, i have to look at my “cheat sheet” to remember some simple stuff.
So… as sometimes my files get messy, and hard to find, i’ll be posting some of those “cheats” here.
(This, was practically copied from here : http://sestud.uv.es/manual.esp/asp/asp16.htm )
Server.CreateObject Method
Server.CreateObject(“Scripting.FileSystemObject”)
Using this object, we will have a way to look (and change) to the structure of files/folders on a given system. First, you need to create a FileSystem object, and then, the File or Folder object (i think you can guess which one is for files , or folders)
<%
Set FS = Server.CreateObject("Scripting.FileSystemObject")
Set File = FS.GetFile("C:\ASP\text.txt")
Set Folder = FS.GetFolder("C:\ASP")
%>
* FS is a FileSystem object, which will allow us to access the file system on the server.
* File is a File object, which will allow us to access the properties of a text fiel, called text.txt (under c:\ASP\)
* Folder is a Folder object (duh), and will allow us to access properties of the c:\ASP folder.
Now… lets take a look at the properties, that can be “read only” (R) or “read/write” (RW). I’ll use Object as a wildcard for both, File and Folder.
Objeto.Attributes [=new] Set a new, or show attributes of an object. Values are as follow (you can use either the Name, or the value) :
And now, the methods:
In every case, every object must be closed in the reverse order they were created.
<%
File.Close
Set File = Nothing
Folder.Close
Set Folder = Nothing
FS.Close
Set FS = Nothing
%>
To finish, a full example of a directory listing :
<%
set fs = CreateObject("Scripting.FileSystemObject")
path = Server.MapPath("/")
set folder = fs.GetFolder(path)
For each file in folder.Files
Response.write "Filename : " & File.Name & " <br>"
Next
%>