Skip to content

ASP: Reading directories

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 : https://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 file, 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) :
    • Normal = 0 Normal file. No attributes changed.
    • ReadOnly = 1 Read Only File. RW
    • Hidden = 2 Hidden File. RW
    • System = 4 System File. RW
    • Volume = 8 Volume Label of Disk. R
    • Directory = 16 Folder. R
    • Archive = 32 File has been modified since last backup. RW
    • Alias = 64 Link to another file. R
    • compressed = 128 Compressed file. R
  • Objeto.DateCreated Returns the full date when the object was created. R
  • Objeto.DateLastAccessed Returns the full date when the object was last accessed. R
  • Objeto.DateLastModified Returns the full date when the object was last modified. R
  • Objeto.Drive Returns the drive letter where the object resides. R
  • Objeto.Name [= newname] Returns the name, or sets a new name. RW
  • Objeto.ParentFolder Return the parent folder of the object. R
  • Objeto.Path Returns the full path of the object R
  • Objeto.ShortName Returns the short name (8.3), also called DOS Name. R
  • Objeto.ShortPath Returns the short path (8.3), also called DOS Path. R
  • Objeto.Size With files, returns the size of the File. With folders, return the size in bytes of all files/folders within it. R
  • Objeto.Type Returns a descriptive text of the type of file. I.e. a gif file will return “GIF Image”. R
  • Directorio.Files Returns a file collection, containing all the Files contained in the Folder. This includes hidden and system files. R
  • Directorio.SubFolders Return a folder collection, containing all subfolders, including hidden and system folders.R
  • Directorio.IsRootFolder Returns True if the folder is the root folder. False if it isn’t.
And now, the methods:
  • Objeto.Copy dest[, overwrite] Copies a file or folder to “dest”. The optional “overwrite” flag (True or False), specifies if “dest” will be overwritten if it exists. “dest” cannot contain wildcards.
  • Objeto.Delete option Deletes a file or a folder. If “option” is True, files with readonly attribute will be deleted; False (default) won’t.
  • Objeto.Move dest Moves a file or folder, to “dest”. “dest” cannot have wildcards.

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
%>
Published inASPCoding
Ivn Systems/Software ©2020